【问题标题】:NodeJS express-validator, validationErrors()NodeJS express-validator,validationErrors()
【发布时间】:2017-02-10 23:13:57
【问题描述】:

我在一个简单的 NodeJS 应用程序中需要一些关于 express-validatorconnect-flash 的帮助

我的 usercontroller module.exports.post 中的当前代码具有 req.flash('error', errors) 错误消息有效并且正在使用 connect flash 闪烁,但只是显示 [object Object] 而不是实际消息,我可以做一个 JSON.stringify(errors) 并且我得到错误的 JSON 消息,但我希望它只显示 msg 本身,这是代码:

module.exports.postUpdatePassword = (req, res, next) => {
req.assert('password', 'Password must be at least 4 characters long').len(4);
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);

const errors = req.validationErrors();

if (errors) {
req.flash('error', errors);
return res.redirect('/editaccount');
}

如果我添加 JSON.stringify(errors),这就是当前从 flash 中显示的内容:

[{"param":"password","msg":"Password must be at least 4 characters long","value":""}]

【问题讨论】:

    标签: json node.js flash express


    【解决方案1】:

    试试req.flash('error', errors[0].msg);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2021-12-07
      • 2013-07-11
      • 2021-08-20
      • 1970-01-01
      相关资源
      最近更新 更多