Express Validator Error: expressValidator is not a function

//just pass the checking as middleware not in the callback
//see here I've just passed an array for checking as middleware
// as the middleware is an array therefore you can add multiple checks in the array
router.post("/", [check('email', "your custom error message").isEmail()], (req, res) => {

  const errors = validationResult(req);
  if (!errors.isEmpty()) {
   res.render('errorPage', { errors: errors.array() });
   //if api caller return res.status(422).json({ errors: errors.array() });
  }
  else{
    //here everything is ok to proceed
   res.render('successPage', { data });
   //to api caller  res.json({msg : "ok"})
  }

})

Express Validator has been updated therefore, you can't use it this way This is a new way to use the express validator

Weather stick with the previous version or use the current syntex of it.

npm uninstall express-validator
npm install [email protected]

const { check, validationResult } = require('express-validator');
router.post('/finished', function (req, res) {
let email = req.body.email

check('email', 'Email required').isEmail()

var errors = validationResult(req)
if (errors) {
  req.session.errors = errors
  req.session.success = false
  res.redirect('/email-adress')
  } else {
  req.session.success = true
  res.redirect('/finished')
  }
})

Do this. And remove

app.use(expressValidator()) 

line.


Yeah! Even I had the same problem. You can change the version by writing the command in root folder.

Command:

npm install [email protected] --save-exact