表达验证return [对象对象]

express validation return [object object]

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 validator return 500 [object Object] while console does correct print

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

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

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

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

  next();
};

试试这个应该有用