【问题标题】:TypeError: Cannot read property 'express-validator#contexts' of undefinedTypeError:无法读取未定义的属性“express-validator#contexts”
【发布时间】:2021-07-19 23:16:59
【问题描述】:

我正在尝试使用 express-validator 中间件 v6.10.1 来验证一些数据,但是当我使用 postman 对其进行测试时,结果出现了一些异常错误。

我的代码:

const { check, validationResult } = require('express-validator')

router.post('/', [
  check('name', 'Required Field!').not().isEmpty(),
  check('email', 'Required Field!').not().isEmpty(),
  check('email', 'Not Valid Email Format!').isEmail(),
  check('password', 'Required Field!').not().isEmpty(),
  check('password', 'Password Must Contain At Least 10 Characters!').isLength({ min: 10 }),
  check('type', 'Required Field!').not().isEmpty()
], (req, res) => {
  const errors = validationResult()

  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() })
  }
  res.send('users route...')
})

控制台中显示的错误:

TypeError: Cannot read property 'express-validator#contexts' of undefined

谁能为我解释这个错误并帮助我解决它?

【问题讨论】:

    标签: javascript express postman express-validator


    【解决方案1】:

    根据documentationvalidationResult函数需要一个参数:

    const errors = validationResult(req)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-03
      • 2013-01-19
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多