Javascript Mongoosejs:如何将节点验证器对象传递给查询的回调函数
我一直在努力将变量/函数传递给mongoose查询回调函数 我想做的是: 编辑:更改标题并重新定义示例Javascript Mongoosejs:如何将节点验证器对象传递给查询的回调函数,javascript,mongoose,Javascript,Mongoose,我一直在努力将变量/函数传递给mongoose查询回调函数 我想做的是: 编辑:更改标题并重新定义示例 Validator.protoptype.customCheck = function(model, path) { var self = this // validator object {msg: 'msg', error: ...}; model.find({name: 'john'}, function(err, res) { if (res.length ===
Validator.protoptype.customCheck = function(model, path) {
var self = this // validator object {msg: 'msg', error: ...};
model.find({name: 'john'}, function(err, res) {
if (res.length === 0) {
self.error( self.msg ) // msg hasn't the expected value.
// As I have some other validators after this callback gets executed
// the context is different. don't know how to pass the right context
} else {
// something else ...
}
}
}
为了提供更多细节,我想使用的实际上下文是为检查值的唯一性创建一个自定义验证器;你看到的具体问题是什么?@JohnnyHK:你是对的,事实上我试图从自定义的“验证器”函数传递
这个
。问题是,在执行回调之前,上下文发生了变化(在两者之间调用了另一个验证器)。。。我完全迷路了。如果您需要更精确的示例来说明我想要做什么,请告诉我。在异步find
回调发生时,您的同步customCheck
验证函数将已返回,因此届时验证程序对象不再有效。您不能。您需要使用支持异步的东西。