bcryptjs compare 2021 problem code example

Example: bcryptjs compare 2021 problem

Somehow it used to work before feb 2021 but afterwards it just bugged...

For People like me who uses Bcryptjs for hashing / Crypting password 
that they pass to MongoDB:

If you have a:

 userSchema.pre("save", async function (next) {
	if (!this.isModified("password")) return next();
    const salt = await bcrypt.genSalt(10);
	this.password = await bcrypt.hash(this.password, salt);
	next();
 });

then add a this.isNew like: 
	
 userSchema.pre("save", async function (next) {
	if (!this.isModified("password") || this.isNew) return next();
    const salt = await bcrypt.genSalt(10);
	this.password = await bcrypt.hash(this.password, salt);
	next();
 });

Checkout the StackOverflow answer by Samso Maosa

Tags:

Misc Example