I have a user schema where I want to update some info, like this.
User.findOne({_id: idd}, function(err, usr){
usr.info = "some new info";
usr.save(function(err) {
});
});
But the model has a hook on save to hash the password
UserSchema.pre('save', function(next) {
if (this.password && this.password.length > 6) {
this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
this.password = this.hashPassword(this.password);
}
next();
});
Now when I try to save it takes the allready hased password and hash it again, any idea how to avoid this?
Use Model.Update and move the creation of a new password to an independent function.
var salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');;
var newPassword = this.hashPassword("someNew password");
User.update({_id: idd}, {
info: "some new info",
password: newPassword
}, function(err, affected, resp) {
console.log(resp);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With