📅  最后修改于: 2022-03-11 14:57:33.690000             🧑  作者: Mango
import bcrypt from 'bcryptjs'
userSchema.pre('save', async function (next) {
if (!this.isModified('password')) { // if the password is not changed we don't want to hash it again
return next()
}
const salt = await bcrypt.genSalt(10) // creates salt
this.password = await bcrypt.hash(this.password, salt) //generates hash and combines it with the salt
})