title: Nodejs, bcrypt async, mongoose login
body: <p>I have the following code for the <strong>user model</strong></p>
  
  <pre><code>const userSchema = mongoose.Schema({
  
  _id: mongoose.Schema.Types.ObjectId,
  username: {
      type: String,
      required: true,
      trim: true,
      minlength: 3,
  
  },
  email: {
      type: String,
      required: true
  },
  password: {
      type: String,
      required: true
  }
  
  })
  </code></pre>
  
  <p>Then for <strong>hashing the password</strong> i have this code</p>
  
  <pre><code>UserSchema.pre('save', function (next) {
  
  
  if (this.isModified('password')){
  
      bcrypt.genSalt(10, (err, salt)=&gt;{
  
          bcrypt.hash(this.password, salt, (err, hash)=&gt;{
              this.password = hash;
              next();
          })
  
      });
  }
  next();
  
  })
  </code></pre>
  
  <p>Then for checking the <strong>hash against the input value from user</strong> i have the following code</p>
  
  <pre><code>userSchema.statics.comparePassword = function(password){
      let user = this;
      return bcrypt.compareAsync(password, user.password)
  }
  </code></pre>
  
  <p>So when it comes to the <strong>usage of all</strong> of these pieces of code i have the following</p>
  
  <pre><code>async loginUser(req, res) {
      try{
          const {email, password} = req.body
          const user = await User.findOne({
              email: req.body.email
          })
          if(!user){
              return res.status(403).send({
                  error: "Incorrect details email"
              })
          }
          const isPassValid = await user.comparePassword(password)
         }catch(err){
          res.status(403).send({
              error: "The bid deal happened"
          })
       }
  }
  </code></pre>
  
  <p>So I've tried searching on google and on this forum to find the answers but everything seems to be outdated or not working particularly for my situation. This code always sends "The bid deal happened", i've tried to debug it from all sides but its still unsuccessful. </p>
  
  <p><strong>The question is</strong> how to make it work? so that i can compare the passwords in the right way</p>
  
  <p><strong>P.S</strong> I've tried changing from compareAsync to compareSync, shows no effect</p>