Reputation: 504
I have something like that
UserSchema.pre('save', function(next) {
console.log(this.isModified());
next();
});
The output is always true
, reading the documentation it says:
Returns true if this document was modified, else false.
I can't find a case when the function returns false
.
UPDATE
This is the way I call the save
function
let user = new User;
user.email = req.body.email;
user.displayName = req.body.displayName;
user.avatar = req.body.avatar;
user.password = req.body.password;
user.save((err, userStored) => {
if (err)
return res.status(500).send({Message: `Error. ${err}`});
res.status(200).send({product: userStored});
})
Upvotes: 1
Views: 1796
Reputation: 294
This is possible because you are always modifying that data. If you want to check that (isModified === false) then try to update document with previous value(same value as before). You will get a false in nModified.
Like,
let updatedata = {
'nOtp': nOtp
};
Model.update({_id: 'your_id'}, { $set: updatedata }, (error, doc) => {
if (error) return res.status(500).json(error);
// **Here below line you can check. If values is same as before**
if (doc.nModified === 0) return res.status(417).jsonp({message: 'document is not modified'});
return res.status(200).json({message: 'success'});
});
You can do it same in your method
Upvotes: 3