I am trying to retrieve the data from multiple collections using mongoose schema and then send the respond back in JSON format. This is the code I have written.
function GetConnections_V2(req,res){
Connection.find({"user_id":"55006c36c30f0edc5400022d"})
.exec(function(err,connections){
var list = [];
var obj = new Object();
connections.forEach(function(connection){
obj.friend_id = connection.user_id_friend;
User.findById(connection.user_id_friend)
.exec(function(err,user){
if(user !== null) {
obj.friend_email = user.email;
obj.friend_details_id = user.details;
UserDetail.findById(user.details).exec(function (err, details) {
obj.firstname = details.firstname;
obj.lastname = details.lastname;
obj.image = details.image;
list.push(obj);
});
}
});
});
});
console.log(list);
res.send(list);
};
Now on executing this code. It is returning empty array. How do I resolve this problem ?