我正在尝试在 Mongoose 查询返回的 JSON 数组上添加一个新键
find({chatBotId:req.params.chatBotId}).
populate('userPlan').
.exec(function(err,result){
result.map(function(e){
e.transactionObject =null
e.taxAmount = 100;
return e;
});
})
我正在添加一个新键taxAmount
,但它没有出现在数组中,但是transactionObject=null
工作正常,它是一个现有键
最佳答案
对我有用的是做 result = JSON.parse(JSON.stringify(result))
或做:
result = result.map(function (e) {
e = e.toJSON(); // toJSON() here.
e.transactionObject = null;
e.taxAmount = 100;
return e;
});
或者更好地使用lean()
(https://mongoosejs.com/docs/api.html#query_Query-lean):
Model.find().lean()
关于javascript - Mongoose 对象 :javascript map function not working on adding new keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49274764/