【问题标题】:express validation return [object object]快速验证返回 [object object]
【发布时间】:2021-11-03 17:40:44
【问题描述】:
import {check, validationResult} from 'express-validator';

export const validate = (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    throw new Error(errors.array());
  }

  next();
};

router.post(
  '/register',
  [
    check('first_name').exists(),
    check('last_name').exists(),
    check('username').exists(),
  ],
  validate,
  (req, res) => {
    return res.json({status: 'success', message: 'Ok'});
  },
);

express 验证器在控制台打印正确时返回 500 [object Object]

[
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'last_name',
    location: 'body'
  },
  {
    value: undefined,
    msg: 'Invalid value',
    param: 'username',
    location: 'body'
  }
]

我们如何解决这个问题,请指导它应该将收到的内容打印到控制台中

【问题讨论】:

    标签: node.js express express-validator


    【解决方案1】:

    请使用响应更新您的中间件或创建错误中间件

    export const validate = (req, res, next) => {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        res.json({error: errors.array()})
      }
    
      next();
    };
    

    试试这个应该可以的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-07
      • 2013-09-13
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2015-07-16
      相关资源
      最近更新 更多