title: How to use a custom express-validator?
body: <p>I'm developing a register form and I'm using <code>express-validator</code> to validate fields but I'm having troubles in validate if email already exists.</p>
  
  <p>I did a select to check if the inserted email already exists but it returns me always that email exists.</p>
  
  <p>I tried other ways but didn't work.</p>
  
  <p>This is my <code>post request</code>:</p>
  
  <pre><code>routes.post('/register', function(req,res)
  {
      var email = req.body.email;
      var username = req.body.username;
      var password = req.body.password;
      var password2 = req.body.password2;
  
      // Validation
      req.checkBody('email', 'Email is required').notEmpty();
      req.checkBody('email', 'Email is not valid').isEmail();
    req.checkBody('email', 'Email already exists').custom(value =&gt; {
      return User.findUserByEmail(value).then(function(user) {
        throw new Error('this email is already in use');
      })
    });
  
      req.checkBody('username', 'Username is required').notEmpty();
      req.checkBody('password', 'Password is required').notEmpty();
      req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
  
      var errors = req.validationErrors();
  
      if(errors){
      console.log(errors);
          res.render('register',{
              errors:errors
          });
      } else {
      console.log("passed");
  
          var newUser = {
              email:email,
              username: username,
              password: password
          };
  
  
          User.createUser(newUser, function(err, User){
              if(err) throw err;
              console.log(User);
          });
  
          req.flash('success_msg', 'You are registered and can now login');
  
          res.redirect('/login');
      }
  });
  </code></pre>
  
  <p>And my function <code>findUserByEmail</code>:</p>
  
  <pre><code>module.exports.findUserByEmail = function(email, callback){
    var connection = db.setDB('DB1');
    connection.then(result =&gt; {
      var request = new mssql.Request(conn);
      console.log("SELECT * FROM u_users WHERE email = '"+email+"'");
      var exists = request.query("SELECT * FROM u_users WHERE email = '"+email+"'");
  
      Promise.all([exists]).then(function(results)
      {
  
        console.log(results);
        if(results[0].length == 0)
        {
          console.log("1");
          return false;
        }
        console.log("2");
        return true;
      }).catch(function(err)
      {
        console.log("Error verifying email...");
        console.log(err);
      });
    });
  }
  </code></pre>
  
  <p>If I do this way it returns me an error <code>Cannot read property 'then' of undefined</code>.</p>
  
  <p>What am I doing wrong?</p>