title: isModified and pre-save mongoose...Nodejs
body: <p>Hi i want save with hashed password only if password is change, so i used isModified function in pre-save, but its always return false even i changed the password. The reason that i am trying to do this is because i dont want to change and save my password when i change other properties. </p>
  
  <pre><code>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');
      });
  });
  </code></pre>
  
  <p>like here i dont want to change my password when i change my graduated value.</p>
  
  <pre><code>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 &amp;&amp; this.isModified('password')){                                                                                                                                                                                                                                                                                      
          this.password  = bcrypt.hashSync(this.password, bcrypt.genSaltSync(8),null);                                                                                                             
      }
  
      next()                                                                                                                                                                     
  }); 
  </code></pre>
  
  <p>any suggestions?</p>