Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12231

validation should run after all other pre-update hooks

$
0
0

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


Viewing all articles
Browse latest Browse all 12231

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>