gpt4 book ai didi

javascript - Node.js Mongoose "object.newProperty"无法向对象添加新属性

转载 作者:行者123 更新时间:2023-12-01 00:44:57 24 4
gpt4 key购买 nike

我正在使用 mongoose 从 Mongo 获取字段并尝试为返回的结果分配一个新键。但是,当我在分配新 key 后记录对象时,记录的对象中不存在该 key 。

奇怪的是,如果我记录 obj.newKey,它会按预期记录,但在记录整个对象时似乎缺少该对象。

const express = require('express');
const router = express.Router({ mergeParams: true });

const V = require('../../../models/KB/V');

router.post('/fetch-v', async (req, res) => {
const { idSC, idDC, idP, from, to } = req.body.tp;
const nds = Math.ceil((to - from) / 86400000 + 1);
// All set. Now query database for visas:
let vs = await V.find({
idSC,
idDC,
idP
}).exec();
vs.map(v => {
v.nds >= nds ? (v.applicable = true) : (v.applicable = false);
console.log('v.applicable:', v.applicable); // Refer Log 1
});
console.log('vs:', vs); // Refer Log 2
return res.send({ success: true, vs });
});

日志 1

v.applicable: true
v.applicable: false

日志2

{
"success": true,
"vs": [
{
"_id": "5d51acd41255b4499021bffa",
"idSC": "5d4e9273928a933e44ba8b56",
"idDC": "5d4e7318c8562b2bf6e4c33f",
"idP": "5d4e74742e0e3c2d2ab5428e",
"nds": 89,
"createdAt": "2019-08-12T18:15:48.329Z",
"updatedAt": "2019-08-12T18:15:48.329Z",
"__v": 0
},
{
"_id": "5d51acd41255b4499021bffb",
"idSC": "5d4e9273928a933e44ba8b56",
"idDC": "5d4e7318c8562b2bf6e4c33f",
"idP": "5d4e74742e0e3c2d2ab5428e",
"nds": 2,
"createdAt": "2019-08-12T18:15:48.329Z",
"updatedAt": "2019-08-12T18:15:48.329Z",
"__v": 0
}
]
}

最佳答案

await V.find({...}) 返回一个 mongoose.Document 数组,因此如果您想向其元素添加新属性,可以使用.lean()就在上面。

示例:

let vs = await V.find({...}).lean();

关于javascript - Node.js Mongoose "object.newProperty"无法向对象添加新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57470318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com