【问题标题】:Is it not possible to mutate object returned from mongoose query?不能改变从猫鼬查询返回的对象吗?
【发布时间】:2020-01-22 20:54:36
【问题描述】:

我正在使用 nodeJS 和猫鼬。我想向 mongoose 查询返回的现有对象添加新键。

Poll.findById(eid)
        .then(poll => {
            poll['newField']=['value1', 'value2'];
            res.send(poll);
        })
        .catch(err=>console.log(err))

此代码显示以前的 poll 值,没有新字段。但是当我 res.send(poll["newField"]) 我可以看到 ["value1","value2"] 作为输出。如何使用 newField 制作 res.send(poll) 显示对象?

【问题讨论】:

  • 你试过Poll.findById(eid).lean().then(...)
  • 那行得通。解决了!

标签: node.js json mongoose


【解决方案1】:

添加 .lean() 作为 Poll.findById(eid).lean().then(...) 解决了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 2012-01-09
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2016-07-27
    • 2017-12-03
    相关资源
    最近更新 更多