Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 487
Mohamed Amine Boudagga Avatar asked Mar 31 '26 04:03

Mohamed Amine Boudagga


1 Answers

According to the documentation, validationResult function requires an argument:

const errors = validationResult(req)

like image 183
Michael Rovinsky Avatar answered Apr 02 '26 17:04

Michael Rovinsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!