0

I am trying to show the errors upon submitting the register form, but the errors show up in the following manner (where as each line is 1 error):

[object Object]

[object Object]

[object Object]

[object Object]

Validation code

router.post('/register', function(req, res) {
  var name = req.body.name;
  var email = req.body.email;
  var password = req.body.password;
  var password2 = req.body.password2;

  // validation
  req.checkBody('name', 'Name is required').notEmpty();
  req.checkBody('email', 'Email is required').notEmpty();
  req.checkBody('email', 'Email is not valid').isEmail();
  req.checkBody('password', 'Password is required').notEmpty();
  req.checkBody('password2', 'Passwords do not match').equals(req.body.password);

  var errors = req.validationErrors();

  if(errors) {
    res.render('register', {
      errors:errors
    });
    console.log("errors");
  } else {
    console.log('No validation errors');
  }
});

Jade code

if errors
        for each in errors
          p #{each}

I don't think the code in Jade is right, but I don't know how to do it right either.

DowinskiField
  • 133
  • 2
  • 15
  • Just to be sure about the question.. You need to know how to show the errors in jade ? Also having a quick look at the errors, you are doing that wrong. Its a better idea to have your errors as a dictionary with key as field that has error and value as a array of associated errors :) – georoot Mar 14 '17 at 17:10
  • @georoot Yeah I want to show the errors to the user, in the Jade template. And about the dictionary, how would you implement it? – DowinskiField Mar 14 '17 at 17:28

1 Answers1

0

I fixed it with this code.

if errors
      for each in errors
        p.error #{each.msg}

I found the solution by experimenting with this code and after that implementing it in jade.

for (each in errors) {
      console.log(errors[each].msg);
    }
DowinskiField
  • 133
  • 2
  • 15