Javascript Mongoose Schema.pre(';save';)函数不散列我最新更新的密码
我正在尝试更新控制器中的用户密码,但在运行Javascript Mongoose Schema.pre(';save';)函数不散列我最新更新的密码,javascript,node.js,express,mongoose,mongoose-schema,Javascript,Node.js,Express,Mongoose,Mongoose Schema,我正在尝试更新控制器中的用户密码,但在运行.save() 这是我控制器中的代码 const user = await User.findById(token.user_id); user.password = password; await user.save(); 这是我的.pre()函数 UserSchema.pre('save', async function (next) { console.log('we ran .save()'); // this console.log()
.save()
这是我控制器中的代码
const user = await User.findById(token.user_id);
user.password = password;
await user.save();
这是我的.pre()
函数
UserSchema.pre('save', async function (next) {
console.log('we ran .save()'); // this console.log() displayed
try {
if (this.password && this.isModified('password')) {
this.password = await bcrypt.hash(this.password, passwordSaltRound);
console.log('we got here', this.password); // this didn't
}
next();
} catch (err) {
next(err);
}
});
正如您在控制台日志中看到的,它运行了.pre()
,但没有输入条件。
:(无法复制此信息-新密码是否与旧密码不同?如果值相同,则修改后的密码将返回false。