我有2个唯一字段(电子邮件和用户名)的用户架构,当我尝试创建具有重复字段的新用户时,我有错误
{
"error": {
"code": 11000,
"index": 0,
"errmsg": "E11000 duplicate key error collection: InstaClone.users index: email_1 dup key: { : \"baidario.ua@gmail.com\" }",
"op": {
"hashedPassword": "$2a$10$eaMym5AMibJlGOSBV3/DHOXPhSoMMDTNwxGcSOn4tEPg07htyQ2Ey",
"email": "baidario.ua@gmail.com",
"username": "baidario",
"fullName": "Roman Baida",
"_id": "58d3a8acdc57641e30e9027a",
"created": "2017-03-23T10:50:49.188Z",
"__v": 0
}
}
}
我如何获得重复的文件名?我看到的唯一方法是解析errmsg
字段。也许有人知道更好的方式?
答案 0 :(得分:2)
要自定义从mongoose唯一验证返回的错误,您可以使用名为mongoose-beautiful-unique-validation
https://www.npmjs.com/package/mongoose-beautiful-unique-validation