更新mongoose模型(node.js)中的一个字段
我有一个用户模式,我想在其中更新一些信息,如下所示更新mongoose模型(node.js)中的一个字段,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个用户模式,我想在其中更新一些信息,如下所示 User.findOne({_id: idd}, function(err, usr){ usr.info = "some new info"; usr.save(function(err) { }); }); 但是该模型在save上有一个钩子来散列密码 UserSchema.pre('save', function(next) { if (this.password && this.password
User.findOne({_id: idd}, function(err, usr){
usr.info = "some new info";
usr.save(function(err) {
});
});
但是该模型在save上有一个钩子来散列密码
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();
});
现在,当我尝试保存它时,会使用allready hased密码并再次对其进行哈希运算,您知道如何避免这种情况吗?使用并将新密码的创建移动到一个独立的函数
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);
})
您是否尝试使用
isModified
UserSchema.pre('save', function(next) {
if (this.password && this.password.length > 6 && MYMODEL.isModified('password')) {
this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
this.password = this.hashPassword(this.password);
}
next();
});
更新模型已被弃用,请改用updateOne。此处的resp是什么?