Typeerror: expressValidator is not a Function

Asked

Viewed 1,434 times

0

I’m having trouble solving the issue below where the message appears "Typeerror: expressValidator is not a Function"

var express = require('express');
var consign = require('consign');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');

var app = express();

app.set('view engine', 'ejs');
app.set('views', './app/views');

app.use(bodyParser.urlencoded({extended: true}));
app.use(expressValidator());

consign()
    .include('app/routes')
    .then('config/dbConnection.js')
    .then('app/models')
    .into(app);

module.exports = app

1 answer

0


According to the documentation express-Validator

At the time you see the express, Voce has to instantiate as follows:

// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');

app.post('/user', [
  // username must be an email
  check('username').isEmail(),
  // password must be at least 5 chars long
  check('password').isLength({ min: 5 })
], (req, res) => {
  // Finds the validation errors in this request and wraps them in an object with handy functions
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() });
  }

  User.create({
    username: req.body.username,
    password: req.body.password
  }).then(user => res.json(user));
});

I tested it here as follows and it worked:

var { check, expressValidator } = require('express-validator'); app.use(check)

  • I’m sorry, I hadn’t read the Validator documentation, I’ll change the answer.

  • Whoa, buddy, it’s all right now?

  • It worked @Claudio H. Yoshii

Browser other questions tagged

You are not signed in. Login or sign up in order to post.