修改mongoose对象文字结果不起作用

可能重复:
为什么不能修改Mongoose查询返回的数据(例如:findById)

首先,我正在查询到mongoDB,得到所有正确的结果,但只有对对象文字的小修改不起作用。 我正在试图做的,是增加新的领域来评论。 我试图使用DBref方法,但它没有工作,所以我现在做2个查询。

var query = Rss.findOne({ _id: itemId}); query.exec(function(err, feed) { if (!err && feed.comments) { console.log(feed.comments.length); for (var index in feed.comments) { var author = feed.comments[index].author; if (author !== undefined) { User.findById(author, function(err, user) { /**Problem is here **/ feed.comments[index].name = 'Some random field'; console.log('Added new field' + util.inspect(feed)); }); } } } }); 

此外,响应是这个没有缺less.name字段。

 Added new field{ _id: 4f34f343c7b0434217000012, original_link: 'http://com', publish_date: Fri, 10 Feb 2012 10:36:00 GMT, summary: 'some text', title: 'title exampel', comments: [ { body: 'well', author: 4f30265e6f60dc061d000002, _id: 4f34f3b6f96c58541700000f, create_date: Fri, 10 Feb 2012 10:38:46 GMT } ], create_date: Fri, 10 Feb 2012 10:36:51 GMT } 

//编辑更多信息

那么我还没有find答案,但一些如何console.log(feed.comments [index])返回函数的引用。 也许有人对mongoosejs有更多的经验可以解释什么是在这种情况下的解决方法。

 { [Function] author: 'Some random field' } 

你必须告诉mongoose把结果转换成适当的对象。 在修改feed对象之前,只需调用:

 feed = feed.toObject(); 

然后你可以添加所有你想要的附加属性。