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)
}
})
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.