title: Node, best way to check users in db
body: <p>I'd like to ask what is the best way to check if an user already exist and throw an error in Mongo DB. I was trying with express-validator but I don't think that it's a correct practice.</p>
  
  <p>I've been trying with this code and everything is fine except the custom username part. I'm not able to make this code working so maybe there is a better way to do it, not necessarily easier.</p>
  
  <pre><code>router.post('/register', function(req,res) {
          const name = req.body.name;
          const email = req.body.email;
          const username = req.body.username;
          const password = req.body.password;
          const password2 = req.body.password2;
  
          //Looking for errors
          req.checkBody('name', 'Name is required').notEmpty();
          req.checkBody('email', 'Email is required').notEmpty();
          req.checkBody('email', 'Wrong email format').isEmail();
          req.checkBody('username', 'Username is required').notEmpty();
          req.checkBody('password', 'Password is required').notEmpty();
          req.checkBody('password2', 'Passwords do not match').equals(password);
          req.check('username').custom(value =&gt; {
              return User.findOne({username: username}, function(err,result) {
                  if(err) throw err;
                  if(result) {
                      console.log(result.username);
                      return true;
                  }
              });
          });
  </code></pre>