O'Reilly logo
  • Patrick S thinks this is interesting:

In the following example, we are using respond as our callback function, and checking to see whether a user record with the supplied username already exists.

userSchema.path('username').validate(function (value, respond) {
  User.find({username: value}, function(err, users){
    if (err){
      return respond(false);
    console.log('Number found: ' + users.length);
    if (users.length) {
      respond(false); // validation failed
    } else {
      respond(true); // validation passed
}, 'Duplicate username');


Cover of Mongoose for Application Development


check if user exists