我想让密钥电子邮件在该集合中独一无二,但我无法使其正常工作,这是我的服务器代码 .
// Create a schema
var userSchema = new mongoose.Schema({
email: { type: String, required: true},
password: String
});
var userModel = mongoose.model("user", userSchema);
router.post('/postuser', (req, res) => {
console.log('Requested data to server: ' + JSON.stringify(req.body._user));
var user = new userModel({
email: req.body._user.email,
password: req.body._user.password
});
// user.isNew = false;
user.save((err, data) => {
console.log('Analyzing Data...');
if(data) {
console.log('Your data has been successfully saved.');
res.json(data);
}
else {
console.log('Something went wrong while saving data.');
console.log(err);
res.send(err);
}
})
});
注意:我也尝试 email: { type: String, required: true, unique: true}
但它不工作并显示以下错误 .
name:'MongoError',消息:'E11000重复键错误集合:hutreservationsystem.users索引:_Email_1 dup key:{:null}',driver:true,代码:11000,index:0,errmsg:'E11000重复键错误集合:hutreservationsystem.users index:_Email_1 dup key:{:null}',getOperation:[Function],toJSON:[Function],toString:[Function]}
3 回答
Async Custom Validator
您可能希望将验证程序代码更改为es6 .
我实现了以下代码以查看是否有任何错误:
我确保在我的本地MongoDB数据库中没有
users
名称的集合 . 此外,我使用Postman在http://localhost:3000
向我的服务器发送API请求 . 似乎没有问题,因为我继续添加具有不同电子邮件值的用户 . 当我输入具有重复值的电子邮件时,我只收到以下错误这些是我发送的JSON请求:
上面提到的错误是在最后一次请求时发回的,因为重复了电子邮件
hot@mail.com
. 如果您查看链接http://mongoosejs.com/docs/api.html#schematype_SchemaType-unique,则'll see that the E11000 error is only sent when the email entered is not unique. Moreover, your email can' t为空字符串或不存在,因为它违反了必需的属性 .A short answer using this tool mongoose-unique-validator
在你的模型中
That's it! (Notice unique: true)
现在,您的收藏中没有电子邮件重复 .
奖金! :你可以访问错误
所以在你的例子中
访问错误>>所以你可以res.send(err.message)>>'验证失败'