'why hashedPassword = await bcrypt.hash(this.password, salt) is not working?

I'm basically trying to just hash a password using bcrypt using async/await but nothing is working... next() is not working and it is not saving the data into the database and even not hashing the password

  const bcrypt = require("bcryptjs")
    userSchema.pre('save', async function (next) {
      try {
        const salt = await bcrypt.genSalt(10)
        console.log(this.email, this.password);
    
        const hashedPassword = await bcrypt.hash(this.password, salt)
    //above line making problem to me... I don't know but below the above line code is not working... plz help me to figure out the mistake
    
        this.password = hashedPassword
        console.log(`the hashed password is ${this.password}`);
    
        next()
    
      } catch (error) {
        next(error)
      }
    })
     


Solution 1:[1]

try this:

const bcrypt = require('bcrypt')

let letBcrypt = async function() {

let salt = await bcrypt.genSalt(10)
console.log('salt:',salt)
const hashedPassword = await bcrypt.hash('ali', salt)
if(!hashedPassword ){
// something went wrong
  console.log('something went wrong')
} else {
 // successful
  console.log('hsashedPass:',hashedPassword)
}
  
}

letBcrypt();
  

result:

salt: $2b$10$9btuRjCf/ddGsHG9qCIABu
hsashedPass: $2b$10$9btuRjCf/ddGsHG9qCIABuS616GHRHDekz8Ub1tKVrgQu.OjEYnWe

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1