Express Validator Error: expressValidator is not a function

前端 未结 6 1055
孤街浪徒
孤街浪徒 2020-12-16 17:57

I\'m trying to install and use express-validator package. I\'ve installed the package version (6.0.0) and then in my server.js file the code is:

const bodyPa         


        
相关标签:
6条回答
  • 2020-12-16 18:30
    app.use(expressValidator());
    

    Replace this line with

    app.use(expressValidator);
    
    0 讨论(0)
  • 2020-12-16 18:38

    Go to package.json change "express-validator": "^6.6.0" to "express-validator": "^5.3.0", manually then run npm i

    0 讨论(0)
  • 2020-12-16 18:40
    //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"})
      }
    
    })
    
    0 讨论(0)
  • 2020-12-16 18:46

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

    Alternatively you can stay with previous version by running this commands

    npm uninstall express-validator
    npm install express-validator@5.3.0
    
    0 讨论(0)
  • 2020-12-16 18:46
    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.

    0 讨论(0)
  • 2020-12-16 18:50

    Yeah! Even i had the same problem it took 6 hrs to find out the resolution i.e,You can change the version by writing the command in root folder (

    • Command

    npm install express-validator@5.3.1 --save-exact

    0 讨论(0)
提交回复
热议问题