快速validation与mongoose

我正在开发我自己的网站使用(angularjs,bootstrap)前端(节点,快递)为后端(MongoDB,mongoose层)的数据库。
我有一个registry格,在这个表格中,我想检查一下,当有人尝试创build新的帐户时,电子邮件还没有被采取,所以我希望我的注册API检查提交的电子邮件是否已经被采取过。

我现在使用这个validation器https://github.com/ctavan/express-validator#validation-by-schema ,这是我的代码:

var vadlidateRegUser= function (req,res,next) { req.checkQuery('UserName','Username must contain letters and numbers only').isAlphanumeric().notEmpty(); req.checkQuery('Email','Email should have a valid syntax eg: example@example.com') .isEmail().notEmpty(); var error = req.validationErrors(); if(!error){ next(); }else { res.json({success:false, message:error}); } } 

现在我想检查一下validation器,如果电子邮件是唯一的,就像这样:

 req.checkQuery('Email','Email should have a valid syntax eg: example@example.com') .isEmail().isUnique(Model Name); 

有什么build议么 ?

express-validator可以检查和清理你的input数据,但不能告诉你的电子邮件是否已经在你的数据库中。

我认为最好的办法是在你的mongoose Schema中使它unique ,并在电子邮件已经存在时处理错误。

 let User = new Schema({ firstname: String, lastname: String, email: { type: String, required: true, unique: true, // ensure unique email validate: [ isEmail, "Email should have a valid syntax eg: example@example.com" ] }, // ... })