我有一个从数据库中获取数据并将其绑定到某个变量的函数:
exports.getAccDoc = function(req, res, send, next) {
var AccDoc = require('../models/accdoc');
AccDoc.find({
startup_id: req.startup.startup_id
}).exec().then(function(accDocs) {
vm.accDocs = accDocs; //I'm using vm in my view
console.log(vm.accDocs); //logs a array that contains one object
console.log(vm.accDocs[0]); //you know
vm.accDocs[0].test = 'TEST'; //trying to add some property to the object
console.log(vm.accDocs[0].test); //logs TEST like it should be
console.log(vm.accDocs[0]); //logs an object, But it doesn't contain test property
send(res); //sending vm to view
});
};
为什么我的对象在向其添加test
属性后没有更改?
(我可以更改属性的现有值,但不能添加新属性)
答案 0 :(得分:2)
因为从Mongoose返回的结果是Mongoose对象而不是普通的javascript对象。您需要使用lean
方法来获取可以使用的普通对象。
AccDoc.find({
startup_id: req.startup.startup_id
}).lean().exec().then(function(accDocs) {
...