In my route I am trying to replace a value in whatever i get from the database before sending it to my view.
In this case I am trying to replace a reference to a user, with the users username instead. But my console.log's just keep returning the value unchanged.
app.get('/message/inbox', function(req, res) {
var _id = req.user._id;
var inbox = null;
var users = null;
async.parallel([
function(callback){
Inbox.find({to: _id}, callback);
},
function(callback){
User.find({}, callback);
}
],
function(err, result){
inbox = result[0];
users = result[1];
for(var i = 0; i < inbox.length; i++) {
for(var j = 0; j < users.length; j++) {
if(String(inbox[i].from) == String(users[j]._id)) {
inbox[i].from = users[j].local.username;
console.log(users[j].local.username);
console.log(inbox[i].from);
break;
}
}
}
console.log(inbox);
});
});
This is what my console returns:
some@email.com
540468daeb56d5081ade600d
[ { _id: 54084cacf212815422aabe94,
from: 540468daeb56d5081ade600d,
to: 5406bf4c8a8acc88120922dc,
headline: 'Some Headline',
text: 'Some text',
__v: 0 } ]