Thursday, 8 June 2017

How do I iterate through SequelizeValidationError?

I am using NodeJS v6.9.1 with the seqelize 4.0.0 ORM, with pug view engine and creating a login system. I am using express-validator for form validation, but I'm having an issue getting SequelizeValidationError sent to the rendered page in a usable format.

express-validation messages:

{ param: 'name', msg: 'Name field is required', value: '' },
{ param: 'email', msg: 'Email field is required', value: '' },
{ param: 'email', msg: 'Email address is invalid', value: '' },
{ param: 'password', msg: 'Password field is required', value: '' },
{ param: 'password2', msg: 'Password Confirmation field is required', value: '' }

In the pug template, they are called with:

if errors
  each error, i in errors
    div.li.alert.alert-danger #{error.msg}

That all displays fine.

The SequelizeValidationError is in this format:

{ name: 'SequelizeValidationError',
  errors:
   [ ValidationErrorItem {
       message: 'name cannot be null',
       type: 'notNull Violation',
       path: 'name',
       value: null },
     ValidationErrorItem {
       message: 'password cannot be null',
       type: 'notNull Violation',
       path: 'password',
       value: null }
   ]
}

If I add the caught errors to a error array, logging to console results in:

name: 'SequelizeValidationError',
errors: [ [Object], [Object], [Object] ] } ]

I tried changing my pug template to the following:

if errors
  for error in errors
    div.li.alert.alert-danger #{error.message}

And it receives the errors and renders as:

notNull Violation: name cannot be null, notNull Violation: password 
cannot be null

But now the express-validator messages won't render.

The entire code for the route:

app.post('/register', (req, res, next) => {
  var name = req.body.name;
  var email = req.body.email;
  var password = req.body.password;
  var password2 = req.body.password2;
  req.checkBody('name', 'Name field is required').notEmpty();
  req.checkBody('email', 'Email field is required').notEmpty();
  req.checkBody('email', 'Email address is invalid').isEmail();
  req.checkBody('password', 'Password field is required').notEmpty();
  req.checkBody('password2', 'Password Confirmation field 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 {
    User.findOrCreate({
      where: {
        email: req.body.email
      },
        defaults: {
        name: req.body.name,
        password: req.body.password
      }
    }).spread(function(user, created) {
      console.log(user);
      console.log(created);
      if (created == true) {
        res.render('login', {
          created: created
        });
      } else {
        var errors = [];
        var error = {param: "email", msg: "Email address in use: ", value: req.body.email};
          if (!errors) {
              errors = [];
          }
          errors.push(error);
          res.render('register', {
            errors: errors
          });
      }
    }).catch(function(error) {
      var errors = [];
      errors.push(error);
      console.log(errors);
      res.render('register', {
        errors: errors
      });
    });
  }
});

So the question is how can I return both types of error messages to the same handler for use in my rendered page?



via Crazy_Redneck

No comments:

Post a Comment