猫鼬异步模式验证不起作用 [英] Mongoose asynchronous schema validation is not working
本文介绍了猫鼬异步模式验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码可验证我的时区"字段:
I have the following code which validates my "timezone" field:
orgSchema.path('timezone').validate(function(value) {
return Timezone.findOne({_id: value}, "_id", function (err, timezone) { return false; });
}, "Please provide a valid timezone");
即使我在最里面的函数中添加了"return false",该字段也始终在传递.我知道我在某处缺少回调-非常感谢您的帮助.
The field is always passing, even when I add a "return false" in the innermost function. I know that I am missing a callback somewhere - I would appreciate some help.
推荐答案
一个异步验证器需要接受第二个参数,它是为传递验证的布尔结果而必须调用的回调.
An asynchronous validator needs to accept a second parameter that's the callback it must call to deliver the boolean result of the validation.
orgSchema.path('timezone').validate(function(value, callback) {
return Timezone.findOne({_id: value}, "_id", function (err, timezone) {
callback(timezone != null);
});
}, "Please provide a valid timezone");
这篇关于猫鼬异步模式验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文