在玉中显示表单验证错误

时间:2017-03-14 16:05:29

标签: node.js express messagebox jade4j

我试图在提交注册表单时显示错误,但错误以下列方式显示(每行显示1个错误):

uls

验证码

[object Object]

[object Object]

[object Object]

[object Object]

玉码

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中的代码是正确的,但我不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

我用这段代码修复了它。

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

我通过试验这段代码找到了解决方案,之后在jade中实现了它。

for (each in errors) {
      console.log(errors[each].msg);
    }