I've already called lean() to make sure that I've converted the returned data into a plain object but only stories[x].authorId is not getting added as a property to the current stories[x] object.
Can somebody explain why and provide a solution? Does it have to do with scope or is it that I cannot modify the object within another query?
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});
})