【发布时间】:2020-07-26 18:13:39
【问题描述】:
我正在为 mongoose 中的用户模式条目编写验证。我想在架构中创建两个条目(密码、googleId)中的任何一个,但不是两个条目都是必需的。我想确保用户有密码或 googleId。如何做到这一点?以下是我的架构
const UserSchema = new mongoose.Schema({
password: {
type: String,
trim: true,
required: true,
validate: (value)=>
{
if(value.includes(this.uname))
{
throw new Error("Password must not contain username")
}
}
},
googleId: {
type: String,
required: true
}
});
【问题讨论】: