猫鼬不验证更新的部分

时间:2021-01-09 18:29:37

标签: javascript node.js mongodb mongoose

我创建了一个 userSchema mongoose 模型,这里是我模型的相关部分:

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

但是当我更新保存到数据库的记录时:

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

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

验证不起作用,我尝试添加 {runValidators:true} 或创建像

这样的中间件
userSchema.pre("updatedOne", function (next) {
  this.options.runValidators = true;
  next();
});

但他们都没有解决我的问题。我该如何处理?

0 个答案:

没有答案