我创建了一个 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();
});
但他们都没有解决我的问题。我该如何处理?