为什么用户电子邮件验证失败?Mongoose验证错误:



我想扩展我的UserSchema来验证电子邮件

const userSchema = new mongoose.Schema(
{
username: {
type: String,
},
name: {
type: String,
},
email: {
type: String,
trim: true,
required: [true, 'Please add an email'],
unique: true,
lowercase: true,
},

我安装了电子邮件验证器。

userSchema.path('email').validate((email) => {
if (!validator.validate('email')) { return false; }
if (!email) { return false; }
if (email.length === 0) { return false; }
return true;
}, 'Email must have a valid format!');

我提出了一个POST请求

"username" : "KoleSOmbor",
"name" : "Niki",
"email" : "koprivica@gmail.com",

错误

POST /api/v1/users 500 18.706 ms - 618
ValidationError: User validation failed: email: Email must have a valid format!

为什么?

已解决

if (!validator.validate(email)) { return false; }

工作良好。

最新更新