here is my code snippet. I am trying to get details of my friends.friendsList is in reside within the user collection itself and there i am inserting users's id. So first i am fetching userId
,then fetching each of their details.but my problem is i am not getting the values of friendsDetails
outside the fetchDetailsfunction
. I tried many time. i am new to node and sails js. i think its problem of asynchronous execution. How can i solve this problem?
getFriendsDetails:function(req,res){
var userId = req.param('id');
var friendsDetails=[];
User.findOne({
id: userId
}).exec(function(err,user){
var friendsIds=user.friends;
friendsIds.forEach(function(id){
User.findOne({
id: id
}).exec(function fetchDetails(err,Userdetails){
var obj={
id:Userdetails.id,
name:Userdetails.name,
pro_pic:Userdetails.profile_pic
}
friendsDetails.push(obj);
console.log(friendsDetails);//Here consoling pushed data correctly.
});
console.log(friendsDetails);//here i am getting null array
});
});