I have a function that executes three mongooose commands. 1. Finds list of all partners. 2. Finds list of all users. 3. Update users table.
Problem: Mongoose does it asynchronously and it executes other commands. How do I make it execute in series 1,2,3 and then execute other commands. Below, I have attached a code snippet.
The issue with this is it runs the three mongoose commands aynchronously and calls redirect().
router.post('/login', function(req, res, next) {
Partner.findOne({'groupName': req.session.user.groupnName}), function(error, data) {
var partnerInfo = data;
if(partnerInfo && req.session.user) {
req.session.user.accountID = partnerInfo.accountID;
req.session.user.iamRole = partnerInfo.iamRole;
}
};
var id;
User.find({'username': req.session.user.username}, function(err, data) {
if(data) {
id = data._id;
}
});
User.findByIdAndUpdate(id, req.session.user, function(error) {});
return res.redirect('/home');
});