Its possible to force a route ?
Example:
I have this 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);
});
Problem here is that the 'res.json' line is read before the userB is updated
So i created this other route B:
router.get('/get', middleware.isLoggedIn, function(req, res){
res.json(req.user.notifications);
});
My Ajax:
$.get('/set', function(data){
// I only add a "fa-spin" class here
}).then(function(){
$.get('/get', function(data){
$(data).each(function(i, item){
$('.notDrop').prepend(item);
});
// Remove the "fa-spin" class
});
});
But sometimes route "B" is called before "A" ends;
So i want to know if its possible to call the "B" route only after the "A" one gets totally finished.