【问题标题】:Showing form validation errors in jade在jade中显示表单验证错误
【发布时间】:2017-03-15 04:05:10
【问题描述】:

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

[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');
  }
});

玉码

if errors
        for each in errors
          p #{each}

我不认为 Jade 中的代码是正确的,但我也不知道如何正确。

【问题讨论】:

  • 只是为了确定这个问题..您需要知道如何在jade中显示错误?还快速查看错误,您做错了。将您的错误作为字典,将键作为字段,将错误和值作为关联错误的数组,这是一个更好的主意:)
  • @georoot 是的,我想在 Jade 模板中向用户显示错误。关于字典,你将如何实现它?

标签: node.js express messagebox jade4j


【解决方案1】:

我用这段代码修复了它。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2018-04-19
    • 2014-02-20
    • 2012-08-24
    相关资源
    最近更新 更多