I am new in node and mongodb and I am trying to update the user password in the mongo database using node. I am getting the following error --
errmsg: 'After applying the update to the document {_id: ObjectId(\'5a4f50381a1152ec09277578\') , ...}, the (immutable) field \'_id\' was found to have been altered to _id: ObjectId(\'5a739493c592356f1b5a78b6\')' }
My code in models/accounts.js is
AccountSchema.static('passchange', function (password, callback) {
var that = this;
if (typeof userobj!="undefined") {
console.log("User Object");
console.log(userobj._id); //I am getting the user id here
that.findOne({_id:userobj._id}, function (err, user) {
if (!user) {
console.log("No User");
return callback(null, false);
}
else
{
var salt = this.salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(password, salt);
var updatepwd = new that({
salt: salt,
hash: hash
});
var conditions = { _id: userobj._id }
that.updateOne(conditions,updatepwd, function (err, savePassword) {
if (err)
{
console.log("err");
console.log(err);
return callback({err: err}, false);
} else {
console.log("savePassword._id");
console.log(savePassword._id);
//return callback(null, {_id : savePassword._id});
}
});
}
});
}
});
Any help is highly appreciated. Thanks in advance.
updatepwdshouldn't be{ '$set': { salt: salt, hash: hash } }instead? My understanding is that you're simply overwriting the entire document as opposed to updating specific values. The$setoperator would fix this problem.