【发布时间】:2015-11-14 23:49:12
【问题描述】:
我已经调用了 lean() 以确保我已将返回的数据转换为普通对象,但只有 stories[x].authorId 没有作为属性添加到当前的 stories[x] 对象中。
有人可以解释原因并提供解决方案吗?它与范围有关还是我无法在另一个查询中修改对象?
Story.find({}).sort({created: -1}).lean().exec(function(err, stories) {
if (err) throw err;
for(var x=0; x < stories.length; x++) {
stories[x].commentsLength = stories[x].comments.length;
stories[x].created = stories[x]._id.getTimestamp();
stories[x].timeAgo = timeHandler(stories[x]);
User.find({'username': stories[x].author}).lean().exec(function(x, err, data) {
stories[x].authorId = data[0]._id;
console.log(stories[x]); // authorId is there
}.bind(this, x));
console.log(stories[x]); // authorId is not there
}
res.render('news', {message: "Homepage", data: stories});
})
【问题讨论】:
标签: javascript node.js mongodb mongoose nosql