isModified 并预存猫鼬...Nodejs

Sco*_*Kim 3 mongoose node.js

嗨,我只想在密码更改时使用散列密码保存,所以我在预保存时使用了 isModified 函数,但即使我更改了密码,它也总是返回 false。我尝试这样做的原因是因为我不想在更改其他属性时更改和保存我的密码。

router.post('/changepw', isAuthenticated, function (req, res, next) {
    User.findOneAndUpdate({_id: req.user._id}, {$set: req.body},{ new: true }, function (err, user){

        if (err) {
          return err;
        } 
        else {

          if (req.body.password) {
            user.password = req.body.password;
            user.save();
          } else { 

          }

        }
        res.redirect('/profile');
    });
});
Run Code Online (Sandbox Code Playgroud)

就像在这里,当我更改毕业值时,我不想更改密码。

router.post('/edit', isAuthenticated, function (req, res, next) {
    User.findOneAndUpdate({
        _id: req.user._id
    }, {
        $set: {
            name: req.body.name,
            phone: req.body.phone,
            classc: req.body.classc,
            major: req.body.major,
            minor: req.body.minor,
            linkedin: req.body.linkedin,
            bio: req.body.bio
        }
    }, {
        new: true
    }, function (err, user, done) {

        if (err) {
            return err;
        } else {

            if (typeof req.body.graduated == 'undefined') {
                user.graduated = false;


            } else if (typeof req.body.graduated == 'string') {

                user.graduated = true;

            }

            user.save();
        }
        res.redirect('/profile');
    });
});
userSchema.pre('save', function(next) {
console.log(this.isModified('password'));                                                                                                                                        
    if(this.password && this.isModified('password')){                                                                                                                                                                                                                                                                                      
        this.password  = bcrypt.hashSync(this.password, bcrypt.genSaltSync(8),null);                                                                                                             
    }

    next()                                                                                                                                                                     
}); 
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 7

请注意,当您使用findAndUpdate()方法时,不会触发预保存挂钩。使用新钩子检查 Mongoose 文档:http : //mongoosejs.com/docs/middleware.html#notes


Moh*_*her 5

尝试这个

userSchema.pre('save', async function (next) {
  // Only run this function if password was moddified (not on other update functions)
  if (!this.isModified('password')) return next();
  // Hash password with strength of 12
  this.password = await bcrypt.hash(this.password, 12);
  //remove the confirm field 
  this.passwordConfirm = undefined;
});
Run Code Online (Sandbox Code Playgroud)

  • 这不会在 `User.findOneAndUpdate` 期间被调用。 (2认同)