title: How to push and error from mongodb routes to validator, user signup form
body: <p>I have a user sign up page in react, if a user tries to signup with an already taken screenname I get a mongodb error, since i set the scheme to screenname unique, Im validating the form using validator, how can i push "this username has already been taken" in errors.screenname in my validator file</p>
  
  <p>/validator.js :</p>
  
  <pre><code>  const isEmpty = require('lodash/isEmpty');
    const validator = require('validator');
  
  
    function validateInput(data) {
     let errors = {};
  
      if(validator.isEmpty(data.screenname)) {
       errors.screenname = 'This field is require';
     }
  
     if(validator.isEmpty(data.email)) {
        errors.email = 'This field is require';
     }
  
     if(!validator.isEmail(data.email)){
       errors.email = 'Email is not valid!';
     }
  
    if(validator.isEmpty(data.password)) {
       errors.password = 'This field is require';
     } 
  
     if (!validator.equals(data.password, data.passwordConfirmation)){
       errors.passwordConfirmation = 'Passwords must match!';
    }
  
     if(validator.isEmpty(data.passwordConfirmation)) {
        errors.passwordConfirmation = 'This field is require';
     }
  
     return{
       errors,
       isValid: isEmpty(errors)
      }
     }
  
     module.exports = validateInput;
  </code></pre>
  
  <p>User route file :</p>
  
  <pre><code>   const express = require('express');
     const bcrypt = require('bcrypt');
      const UserDB = require('../../model');
     const commonValidations =   require('./shared/validation/signupvalidation');
  const Promise = require('bluebird');
  const isEmpty = require('lodash/isEmpty');
  
   var mongoose = require('mongoose');
  
   // mongoose.connect("mongodb://localhost:27017/RockfellerDB");
   var db = mongoose.connection;
  
     let router = express.Router();
  
    function validateInput(data, otherValidations) {
      let { errors } = otherValidations(data);
  
     return Promise.all([
        router.get('/', function(req, res){
          UserDB.find({ screenname: data.screenname }).then(user =&gt; {
                if (user) { errors.screenname = 'This username is already taken'         }
            })
        })
     ]).then(() =&gt; {
      return {
          errors,
          isValid: isEmpty(errors)
           }
       })
  
       }
  
  
     router.post('/', (req, res) =&gt; {
         validateInput(req.body, commonValidations).then(({ errors, isValid }) =&gt; {
  
         // if form is Valid we send it to our mongo database
           if (isValid){
              const password_digest = bcrypt.hashSync(req.body.password, 10);
  
               var newUser = new UserDB({ 
                 screenname : req.body.screenname,
                  email: req.body.email,
                 password: password_digest 
              }) 
  
              newUser.save(function(err, doc){ 
                 if (err) { 
                     console.log(err);
                 } else {
                     res.send(doc);
                 }
             });
  
           } else {
              res.status(400).json(errors);
          }
        })
    });
  
  
     module.exports = router;
      return router;
  </code></pre>