mercredi 18 février 2015

Pushing object into mongoDB, ["object Object"] is saved


Sorry if this is a repetitive one, I really couldn't solve it with any of the information provided here.


Basically I have this in my User.js mongoDB schema:



notifications: [{
type: String,
story: String,
seen: Boolean,
createdTime: Date,
from: {
name: String,
username: String,
id: mongoose.Schema.ObjectId
}
}]


After I query the user, this is what I do to push this object:



var notifObj = {

type: notification.type,
story: notification.story || ' ',
seen: false,
createdTime: new Date(),
from: {
name: notification.from.firstName + " " + notification.from.lastName,
username: notification.from.username,
id: notification.from._id
}
};


into the mongoDB database:



user.notifications.push(notifObj);

User.update({
_id: notification.to
}, user, function(err, data) {

if (err) {

deferred.reject({
err: err
});
}

//Tell sender everything went alrgiht
deferred.resolve(data);
});


P.S.: I have deferred.resolve instead of res.end(), because I push notifications on some of the requests in a different controller, I don't have a separate route only for notifications. (e.g.: User has a new message, I send the message and push a notification too)





Aucun commentaire:

Enregistrer un commentaire