我创建了这样的猫鼬模式:
import mongoose from "mongoose";
import bcrypt from "bcryptjs";
const Schema = mongoose.Schema;
const UserSchema = new Schema({
password: {
type: String,
select: false,
required: true
},
role: {
type: String,
trim: true,
required: true
},
email: {
type: String,
trim: true,
index: true,
unique: true,
match: [/.+\@.+\..+/],
required: true
},
username: {
type: String,
trim: true,
index: true,
unique: true,
required: true
},
surname: {
type: String,
trim: true,
required: true
},
name: {
type: String,
trim: true,
required: true
}
});
export default mongoose.model("user", UserSchema);
我的问题是我需要以向后模式声明字段,因为当我尝试将用户保存到数据库中并且名称字段为空时,如果首先更改密码字段的位置,则名称字段的错误将首先出现到最后一个字段,然后是该字段所需的错误,第一个将出现密码,我不知道为什么会这样。