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

Viewed 650

I am trying to validate some data using express-validator middleware v6.10.1 but it turns out some unusal error when I am testing it with postman.

My code:

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...')
})

The error that shows in the console:

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

Anyone can explain for me this error and help me to fix it ?

1 Answers

According to the documentation, validationResult function requires an argument:

const errors = validationResult(req)

Related