javascript - 类型错误 : Cannot read property 'express-validator#contexts' of undefined

标签 javascript express postman express-validator

我正在尝试使用 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

任何人都可以为我解释这个错误并帮助我修复它?

最佳答案

根据documentation , validationResult 函数需要一个参数:

常量错误 = validationResult(req)

关于javascript - 类型错误 : Cannot read property 'express-validator#contexts' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67269061/

相关文章:

javascript - 将现有元素注入(inject) React

javascript - 如何访问 Handlebars 中具有动态索引的对象中的数组?

javascript - Angular 拼接功能删除另一条记录而不是实际选择的记录

node.js - 表不存在 Sequelize

docker - 无法使用 Docker 和环境文件运行 Postman 远程 URL

javascript - 基于排序数组更新组件。 Angular (4.3)

javascript - 为什么不将数据库查询的结果推送到变量中?

node.js - PassportJS : How to get req. 用户在我看来

R httr 包 - 使用 body 向 API 发出 POST 请求

javascript - 在 Node.js 中读取 JSON 请求值