title: Express and node send error messages to handlebars view
body: <p>How do I output validation errors to the view for the email <code>unique: true</code> option?  In my handlebars view I am getting <code>errors</code> passed from <code>var errors = req.validationErrors();</code> and displaying it in the view which is working. but the email validation for uniqueness is going to <code>user.save((err)</code> and is just being sent to the console.  The validation is working correctly, no user is created if there is a duplicate email.  I'm just trying to send the error message to the view.  Can I do a <code>req.check</code> for <code>unique: true</code> ?      </p>
  
  <pre><code>const UserSchema = new Schema({
      email: { type: String, index: { unique: true } },
      password: { type: String, required: true }
  });
  </code></pre>
  
  <p>my createUser.handlebars view</p>
  
  <pre><code>{{#if errors}}
    &lt;section class="errors"&gt;
      &lt;ul&gt;
        {{#each errors}}
          &lt;li&gt;{{this.msg}}&lt;/li&gt;
        {{/each}}
      &lt;/ul&gt;
    &lt;/section&gt;
  {{/if}}
  </code></pre>
  
  <p>the createUser function</p>
  
  <pre><code>module.exports.createUser =
      (req, res, next) =&gt; {
  
          let user = new User({
              email: req.body.email,
              password: req.body.password
          });
  
  
          req.check('email', 'Invalid email').isEmail(); 
          req.check('password', "Passwords must match").isLength({min: 6}).equals(req.body.passwordConfirmation);
  
          var errors = req.validationErrors();  // save any error messages
  
          // check if there are any errors
          if (errors) {
              req.session.errors = errors; 
  
              req.session.success = false;
              return res.redirect('/users/new'); 
          } else {
              req.session.success = true;
          }
          // save the user
          user.save((err) =&gt; {
              if (err) {
                  console.log("Error : %s ", err);
              }
              // set the session messages back to null and redirect
              req.session.success = null;    
              req.session.errors = null;                    
              res.redirect('/');
          });
      };    
  </code></pre>