Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created a userSchema mongoose model and here is the related part of my model :

  password: {
    type: String,
    required: [true, "Please enter a password"],
    minlength: [6, "Minimum password length is 6 characters"],
  },

but when i updated a record that is saved to database:

userSchema.statics.updated = async function (_id, field, value) {
  const user = await this.updateOne(
    { _id },

    {
      $set: { [field]: value, updatedAt: Date.now() },
    }
  );
};

the validation does not work i tried adding {runValidators:true} or creating a middleware like

userSchema.pre("updatedOne", function (next) {
  this.options.runValidators = true;
  next();
});

but none of them solved my problem. How can i handle that?

question from:https://stackoverflow.com/questions/65646164/mongoose-does-not-validate-the-updated-part

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
246 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...