简体   繁体   English

如何将来自Mongoose的JSON映射到Vue和Quasar?

[英]How to map JSON coming from Mongoose to Vue and Quasar?

I have a Mongoose backend and wrote some REST APIs to supply data to my Vue/Quasar frontend. 我有一个Mongoose后端,并编写了一些REST API将数据提供给我的Vue / Quasar前端。 It's still basic, uses Node/Express http for the API calls, no Axios or the like yet. 它仍然是基本的,使用Node / Express http进行API调用,还没有Axios等。 I got some simple CRUD get/put/post/delete APIs working, but now have a more complex one that is based on a specific Mongoose query to return me the children of a node. 我得到了一些简单的CRUD get / put / post / delete API,但是现在有一个更复杂的API,它基于特定的Mongoose查询向我返回节点的子代。

Mongoose provides me the data in this format: 猫鼬以以下格式向我提供数据:

{“kinder”: [ {name: value, …}, {name: value, …} ] }  

which seems to be an array wrapped into an object. 这似乎是包装在对象中的数组。 I couldn't figure out how to change that format on the Mongoose/backend side. 我不知道如何在Mongoose /后端更改该格式。

Vue and Quasar at the frontend need to consume some (not all) of these values coming in from Mongoose, but they want it as a pure array in the form of 前端的Vue和Quasar需要消耗一些(不是全部)来自Mongoose的值,但他们希望将其作为纯数组,形式为

[ {name: value, ...}, {name: value, ...} ]

And I need to insert some new name: values pairs for Quasar's use, in addition to what is coming in from the backend.. 而且我需要插入一些新名称:除了来自后端的内容外,还需要Quasar使用的值对。

After doing my REST call from the Vue component's onLazyload method, to get the children from the backend, I can see the children perfectly in the Chrome Vue Tools, so I think the REST API call works, as designed. 在通过Vue组件的onLazyload方法执行REST调用之后,为了从后端获取子级,我可以在Chrome Vue Tools中完美地看到这些子级,因此我认为REST API调用按设计工作。 It works also perfectly directly through localhost:8080/api/baustoff/kinder/<_id>, delivering the children in the above format. 它也可以直接通过localhost:8080 / api / baustoff / kinder / <_ id>完美地工作,以上述格式交付子级。

My problem comes in the mapping of the REST response data to what is needed by Vue/Quasar, code that is also in onLazyLoad method, after the REST call (see below). 我的问题来自REST调用后,REST响应数据到Vue / Quasar所需的映射,该代码也在onLazyLoad方法中。

I tried various changes to the code below, eg using JSON.parse, as suggested elsewhere, but failed. 我尝试对下面的代码进行各种更改,例如使用JSON.parse(如在其他地方建议的那样),但失败了。 I believe the main issue is how to deal with this nested array? 我相信主要问题是如何处理此嵌套数组?

Here is the code on the Vue component's side (onLazyLoad method) which is not working: 这是不起作用的Vue组件一侧的代码(onLazyLoad方法):

onLazyLoad({ node, key, done, fail }) {
      let parent_id = node._id; 
      // REST call to get the children of a parent node from Mongoose
      http
        .get("/baustoff/kinder/"+parent_id)
        .then(response => {
            // If no children, return empty tree array
          if (response.data == null) {
            done([]);
            return;
          }
          // Process to create Vue/Quasar QTree array with data from REST
          // PROBLEMATIC CODE STARTS PROBABLY HERE!!!!!!!!!!!!!!!
          let donvis = response.data;
          let treeNodes = []; // array to prep the children as needed by QTree 
          for (let i = 0; i < donvis.length; i++) {
                 treeNodes[i] = {
                 fachlicherTyp: donvis[i].fachlicherTyp,
                 _id: donvis[i]._id,
                 lazy: true,
                 parent_id: parent_id, 
               };    
          }
          done(treeNodes); //Draw tree by Quasar/vue QTree
          this.treeChange++; // Marking tree change to Quasar
          return null;
      })
        .catch(e => {
          console.log(e);
        });
    }, // /onLazyLoad()

The problem: The code never gets into the for loop, since donvis.length is undefined - my fault, since I'm certainly not doing this processing of the response right. 问题:代码从未进入for循环,因为donvis.length是未定义的-这是我的错,因为我当然没有对响应的正确处理。

I would prefer to get a solution in the Vue code above, since I fear I will have to deal with other similar trouble situations. 我宁愿在上面的Vue代码中获得解决方案,因为我担心我将不得不处理其他类似的麻烦情况。

I would also appreciate hints about how to better prepare the JSON output on the Mongoose side, eg get rid of the {"children:" }. 我还希望获得有关如何在Mongoose端更好地准备JSON输出的提示,例如,摆脱{“ children:”}。 The Mongoose query for it is this: 猫鼬查询是这样的:

Baustoff
  .findById(req.params.baustoffId)
  .select('kinder -_id') 
  .populate('kinder','name baumKnotenTyp fachlicherTyp aktiv produkt') 
   .lean().exec ( (err, baustoff) => { callback(err, baustoff, res) })
};

If I understand correctly, donvis is the data from mongoose, which is an object. 如果我理解正确,donvis是来自猫鼬的数据,这是一个对象。 Objects don't have a length property 对象没有length属性

You should try this: 您应该尝试这样:

donvis.kinder.forEach(child => {
  treeNodes.push({
    fachlicherTyp: child.fachlicherTyp,
    _id: child._id,
    lazy: true,
    parent_id: parent_id,
  })
})

I'm using forEach because I think it makes things more readable. 我之所以使用forEach,是因为我认为它使内容更具可读性。 Tell me if you don't know how to use it. 告诉我您是否不知道如何使用它。

Notice that I'm also using treeNodes.push(...) instead of treeNodes[i] = ... This is a more standard way of filling up an array 请注意,我也在使用treeNodes.push(...)代替treeNodes [i] = ...这是填充数组的更标准方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM