I have this Ajax route:
router.get('/getThey', middleware.isLoggedIn, function(req, res){
var array = [];
var followingLength = req.user.they.length -1;
req.user.they.forEach(function(userId, i){
User.findById(userId, function(err, foundUser){
foundUser.theySent.forEach(function(theySent, i){
var foundLength = foundUser.theySent.length -1;
while(i !== foundLength){
if(req.user.notifications.indexOf(theySent) === -1){
req.user.notifications.push(theySent);
req.user.save();
}
}
});
});
});
res.json(req.user.notifications);
});
But the code below runs first because the User findById
if(aux === true && i === followingLength){
res.json(req.user.notifications);
}
What can I do to make findById Synchronous ?