expressValidator is not a function even it specifies middleware must be function

//route.js
> const {body} = require('express-validator');
var validate=(method) =>{
    switch(method){
        case 'userRegistered':{
            return [
                body('username','Username doesnot exists').exists(),
                body('email','Invalid Email').exists().isEmail(),
                body('name','Invalid Email').optional(),
                body('password','password must be atleast 8 characters').exists().isLength({min:8,max:15}),
                body('Image','please upload your Image').exists(),

            ]
        }
    }
}
//route Handler
> router.post('/register-user',uploads.single('Image'),validate('userRegistered'),async(req,res)=>{
console.log(req.file);

const errors = validationResult(req)
if(!errors.isEmpty){
    res.status(422).json({errors:errors.array() });
}
await userRegistered(req.body,req.file.path,"client",res);
});
//app.js
> const expressValidator = require('express-validator');
  const bodyParser = require('body-parser');
  app.use(expressValidator());

// I use express validation as middleware in my REST API but got an error of
// app.use(expressValidator());
^

TypeError: expressValidator is not a function

73 thoughts on “expressValidator is not a function even it specifies middleware must be function”

Leave a Comment