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()
?!
unfortunately I found sails doesn't support array attributes like this in models:
notifications : [
{
type : {
type : 'string',
defaultsTo : 'general'
},
title : {
type : 'string',
required : true
},
link : {
type : 'string'
},
date : {
type : 'datetime',
defaultsTo: new Date().toISOString()
}
}
]
and I forced to change my notifications attribute to:
notifications : {
type : 'array'
}
and check and validate everything manually :(