Javascript 来自RESTful端点的响应与预期不符?

Javascript 来自RESTful端点的响应与预期不符?,javascript,node.js,Javascript,Node.js,我在node.js端点中有以下代码 let nodes = {}; if (req.query.fetchType == "tree") { nodes = await req.groupDocParam.getChildrenTree({ options: { lean: true } }); if(req.query.includeOrganization == "true"){ let data = await Group.findOne({ groupId

我在node.js端点中有以下代码

let nodes = {};
if (req.query.fetchType == "tree") {
    nodes = await req.groupDocParam.getChildrenTree({ options: { lean: true } });
    if(req.query.includeOrganization == "true"){
        let data = await Group.findOne({ groupId: req.groupDocParam.groupId })
        data.children = [...nodes]
        nodes.splice(0, nodes.length) 
        nodes.push(data)
    }
} else if (req.query.fetchType == "children") {
    nodes = await req.groupDocParam.getImmediateChildren({});
}

else {
    nodes = await req.groupDocParam;
}
res.status(200).json({
    message: 'Fetched groups successfully.',
    items: nodes
});
在这四条线上

 let data = await Group.findOne({ groupId: req.groupDocParam.groupId })
 data.children = [...nodes]
 nodes.splice(0, nodes.length) 
 nodes.push(data)
我希望节点数组中包含一个具有新属性
子对象的对象,它确实是这样。但是,当我测试端点时,新添加的
children
属性不会随有效负载一起返回:

期望

items = [
   {
      "id": 21,
      "name": "test"
      // this is from line  data.children = [...nodes]
      "children": [Object, Object]
   }
]
实际的

   items = [
       {
          "id": 21,
          "name": "test"
       }
    ]

nodes对象中是否存在一些不变性问题?

当对数据库执行查询时(在我的例子中是MongoDB),返回的是Mongoose文档,而不是JavaScript对象。因此,无法按原样更改此对象,需要使用lean()方法将其转换为Java脚本对象:

默认情况下,Mongoose查询返回Mongoose文档类的实例。文档比普通的JavaScript对象要重得多,因为它们有很多用于更改跟踪的内部状态。启用精益选项会告诉Mongoose跳过对完整Mongoose文档的实例化,只需提供POJO即可

有了它,我的代码更改为以下内容,并在端点执行时获得了预期的结果

let nodes = {};
let isIncludeOranization = false;

    if (req.query.fetchType == "tree") {
        nodes = await req.groupDocParam.getChildrenTree({ options: { lean: true } });
        if(req.query.includeOrganization == "true"){
            isIncludeOranization = true;
            await Group.findOne({ groupId: req.groupDocParam.groupId })
              .lean()
                .exec(function(err, data){
                   // perform data change here
                   data.children = [...nodes]
                   nodes.splice(0, nodes.length) 
                   nodes.push(data)

                   if(err && !err.statusCode){
                      err.statusCode = 500;
                      next(err);
                   }

                   // return response because we are done
                   res.status(200).json({
                   message: 'Fetched groups successfully.',
                   items: nodes
                });
            });
        }
    } else if (req.query.fetchType == "children") {
        nodes = await req.groupDocParam.getImmediateChildren({});
    }

    else {
        nodes = await req.groupDocParam;
    }

    if(!isIncludeOranization){
        res.status(200).json({
            message: 'Fetched groups successfully.',
            items: nodes
        });
    }

Add.toJson(),将mongoose对象转换为纯JSON。我没有测试它的工作原理,但沿着这条线的东西将是这个最简单的答案

添加
console.log(节点)在响应之前。它是否输出预期值?还有,
nodes.splice(0,nodes.length)
的目的是什么,然后立即推送
data
-为什么不直接推送
nodes=[data]?在响应和数据达到预期值之前,我正在记录节点。我之所以在将数据放入节点之前清空节点,是因为首先填充节点,然后将其数据添加到数据[“子项”],然后再将其添加到节点。“这更多的是数据模型的业务逻辑需求。”Phil刚刚这么做了。基本上,我想向DB调用返回的对象添加一个新属性,但它不会通过响应返回持久化,并且返回没有添加属性的对象。您如何验证“四行”是否实际执行?我建议附加一个调试器,但即使是一些
console.log('here')
行也比nothing@Phil我一步一步地执行代码,看到了变化
 let data = await Group.findOne({ groupId: req.groupDocParam.groupId })
 data = data.toJson();
 data.children = [...nodes]
 nodes.splice(0, nodes.length) 
 nodes.push(data)