Search code examples
node.jsexpressexpress-validator

Check is not defined on express-validator using routes V4


I've been developing an app with expressjs, but came with an error while using express-validator V4. The error is: TypeError: check is not a function

So my code looks like this:

//users.js
var check  = require('express-validator/check');
var validationResult = require('express-validator/check');

router.post('/register',[
    check('name').exists()
], function(req, res){
    var errors = validationResult(req);
    if(errors){
        console.log('There are errors');
    }else{
        console.log('No errors');
    };
});

module.exports = function(app){
    app.use('/users', router);
};

Note that I am using pure javascript, no ES6. Thanks.


Solution

  • express-validator embraces ES6, and it supports Node 6+.
    As you stated in the comments, you're using Node 8.4.0, then you're good with it.

    You're importing the functions wrong, as you must bind the functions from the default export. Or use destructuring, which is a lot cooler:

    const { check, validationResult } = require('express-validator/check');
    
    // or:
    
    const check = require('express-validator/check').check;
    const validationResult = require('express-validator/check').validationResult;
    

    Also, remember to check the usage section :)