The findOne query is returning a user from the DB. When i console.log(user) it does print out user data. But for some reason isn't pushing into user_contacts. Its just returning as an empty array.
get_contacts: function (req, res){
var user_contacts = [];
for(var i=0;i<req.body.contacts.length;i++){
User.findOne({_id: req.body.contacts[i]._User}, function (err, user) {
if(user){
console.log(user);
user_contacts.push(user);
}else{
console.log("Error");
}
})
}
console.log(user_contacts);
res.json(user_contacts);
}
This should be so simple, but just cannot see what I am doing wrong. Appreciate the help.
res.jsonyour DB calls haven't executed yet - hence the empty response.