Is it possible to enforce a specific route?
Scenario:
Consider the following route A:
notiSchema = notification model
router.get('/set', function(req, res){
User.findById("userId", function(err, foundUser){
foundUser.notiSchemaSent.forEach(function(notiSchema, i){
if(req.user.notifications.length === 0){
req.user.notifications.unshift(notiSchema);
req.user.save();
} else {
req.user.notifications.forEach(function(userSchema, i){
if(req.user.notifications.indexOf(notiSchema) === -1){
req.user.notifications.unshift(notiSchema);
req.user.save();
}
});
}
});
});
res.json(req.user.notifications);
});
The issue here is that the 'res.json' line is executed before userB is updated
Therefore, I have created another route B:
router.get('/get', middleware.isLoggedIn, function(req, res){
res.json(req.user.notifications);
});
Here is my Ajax request:
$.get('/set', function(data){
// Adding "fa-spin" class here only
}).then(function(){
$.get('/get', function(data){
$(data).each(function(i, item){
$('.notDrop').prepend(item);
});
// Remove the "fa-spin" class
});
});
However, there are instances where route "B" is called before route "A" is fully completed;
Thus, I am curious to know if it's feasible to trigger route "B" only after route "A" has completely finished processing.