是否可以在Mongoose中进行验证以检查数据库?
我需要这样的东西
var validEmail = require('../helpers/validate/email');
var validDoctor = require('../helpers/validate/doctors');
var schema = mongoose.Schema({
email: { type: string, validate: [validEmail, "invalid email"],
doctor: {type: string, validate: [validDoctor, "invalid doctor"]
}
validDoctor看起来像是:
module.exports = function (doctor) {
Doctors.findOne({email:doctor}, function (err, found) {
return (found);
});
我试图将脚本放在前后挂钩中,而我的代码变得过于草率。我必须得到像这样的验证
答案 0 :(得分:2)
您需要异步验证,它接受第二个参数作为回调函数,使用true
或false
分别表示验证成功或失败
module.exports = function (doctor, done) {
Doctors.findOne({email:doctor}, function (err, found) {
if(found) done(true);
else done(false);
});