0
我已經調用了lean()來確保我已將返回的數據轉換爲普通對象,但只有故事[x] .authorId不會作爲屬性添加到當前故事[x]對象。如何修改從Mongoose查詢返回的數據?
有人可以解釋爲什麼並提供解決方案嗎?它是否與範圍有關,還是我無法修改另一個查詢中的對象?
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});
})