title: Mongoose schema wouldn't call middleware to hash password
body: <p>My Mongoose schema looks like this:</p>
  
  <pre><code>import mongoose from 'mongoose';
  import argon2 from 'argon2';
  import argonConfigs from '../configs/argon-configs';
  
  const { Schema } = mongoose;
  const userSchema = new Schema({
    firstName: String,
    lastName: String,
    googleID: String,
    twitterID: String,
    emails: [String],
    hasPicture: Boolean,
    signupToken: String,
    token: String,
    username: String,
    password: String,
  });
  
  userSchema.pre('save', async function(next) {
    console.log('inside pre');
    if (this.password) {
      console.log('this.password', this.password);
      this.password = await argon2.hash(password, argonConfigs);
    }
    next();
  });
  
  const User = mongoose.model('user', userSchema);
  
  module.exports = User;
  </code></pre>
  
  <p>I amusing this schema to update records in my collection like this:</p>
  
  <pre><code>import User from '../models/user';
  
  export const tokenInDB = async (token, email) =&gt; {
    const existingUser = await User.findOne({ token: token, emails: email, password : { $exists : false } });
    return existingUser;
  };
  
  export const createAccount = async (fname, lname, uname, pass, existingUser) =&gt; {
    let fieldsToUpdate = {};
    if(fname) { fieldsToUpdate.firstName = fname }
    if(lname) { fieldsToUpdate.lastName = lname }
    if(uname) { fieldsToUpdate.username = uname }
    if(pass) { fieldsToUpdate.password = pass }
    const updatedUser = await User.findOneAndUpdate({_id: existingUser._id}, {...fieldsToUpdate, $unset: {token: 1}}, {new: true});
    return updatedUser;
  };
  </code></pre>
  
  <p>I'm expecting the middleware (<code>userSchema.pre()</code>) to kick in every time the update is made. This middleware should then use the <code>argon2i</code> library to hash the plaintext password entered by the user.</p>
  
  <p>However, the middleware refuses to get called. The <code>console.log()</code> inside the function remains uncalled. How do I fix this?</p>