I am trying to update a user document
const userSchema=new mongoose.Schema({ name: String, password: String, confirmPassword: { type: String, validate: { validator: function (val) { if (this instanceof mongoose.Query) { const data = this.getUpdate(); return val === data.$set.password; }else{ return val===this.password; } } }})userSchema.pre("save",async function(next){ this.password=await bcrypt.hash(this.password,12); next();})userSchema.pre("findOneAndUpdate", async function (next) { const data = this.getUpdate(); if (data.password) { data.password = await bcrypt.hash(data.password, 12); } next();});
but what happens is that the password is encrypted before the validation porcess...but if I use the doc.save method it works fine