Mongoose asynchronous circuit checker not working

I have the following code that checks 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"); 

The field always passes even when I add "return false" to the innermost function. I know that somewhere I miss the callback - I would appreciate help.

+6
source share
1 answer
the asynchronous validator must take a second parameter, which should call a callback to deliver a logical result of the check.
 orgSchema.path('timezone').validate(function(value, callback) { return Timezone.findOne({_id: value}, "_id", function (err, timezone) { callback(timezone != null); }); }, "Please provide a valid timezone"); 
+9
source

All Articles