I want to broadcast a notification to all users in one of my services, notifications are array attribute of user model. so I should push object of new notification to every user notification array in database. here is my code:
broadcastNotif : function (notif) {
return new Promise(function (resolve, reject) {
User.find().exec(function (err, users) {
if (err) return reject(err);
else {
while (users.length>0) {
var user = users.pop();
if (user.notifications==undefined)
user.notifications=[];
user.notifications.push({
type : notif.type,
title : notif.title,
link : notif.link,
date : notif.date
});
sails.log(user);
user.save(function (err) {
if (err) return reject(err);
})
}
return resolve();
}
})
})
}
sails.log(user)
print correct objects in console but when I check my mongo Database found nothing saved there, they are all unchanged.
what's wrong with user.save()
?!