1

I am trying to make validation form using Express and node. I am very new to all this and I am getting this error in my console

ReferenceError: expressValidator is not defined

index.js code

var express = require('express'),
 bodyParser = require('body-parser'),
 validator = require('express-validator'),
 routes = require('./routes'),
 path = require('path'),
 ejs = require('ejs'),
 app = express();

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

app.use(expressValidator());

//post
app.post('/signup', routes.registerUser);
app.set('views', __dirname + '/views');

app.set('view engine', 'ejs');
app.set('view options', {layout: false});


//get
app.get('/', routes.index);
app.get('/signup', routes.signup);
app.get('/login', routes.login);

app.listen(3000,function(){
  console.log("Server running at Port 3000");
});

In routes folder i have only one js file index.js

exports.index = function(req, res){
  res.render('index');
};
exports.signup = function(req, res){
  res.render('signup');
};
exports.login = function(req, res){
  res.render('index');
};

exports.registerUser = function(req, res){
    req.check('uname', 'Username is required').notEmpty();
    req.check('fname', 'First name is required').notEmpty();
    req.check('lname', 'Last name is required').notEmpty();
    req.check('email', 'Email is required').notEmpty();
    req.check('mob', 'Mobile Number is required').notEmpty();
    req.check('add', 'Address is required').notEmpty();
    req.check('city', 'City is required').notEmpty();
    req.check('url', 'URL is required').notEmpty();
    req.check('pcode', 'PIN code is required').notEmpty();
    req.check('password', 'Password is required').notEmpty();

    var errors = req.validationErrors();

    if(errors){
        console.log("There may be errors");
        res.redirect('signup');
    }else{
        var uname = req.body.uname;
        var user = {
            'fname' : req.body.uname,
            'lname' : req.body.lname,
            'email' : req.body.email,
            'mob' : req.body.mob,
            'add' : req.body.add,
            'url' : req.body.url,
            'pcode' : req.body.pcode,
            'password' : req.body.password
        };

    }
};

When I run the application I got this every time

app.use(expressValidator()); ^ ReferenceError: expressValidator is not defined

1 Answer 1

2

Change

app.use(expressValidator());

to

app.use(validator());
Sign up to request clarification or add additional context in comments.

8 Comments

can you tell me why app.use(expressValidator()); it's not working.
Because you did not define expressValidator, you import express validator as validattor instead of expressValidator: validator = require('express-validator')
var errors = req.validationErrors(); is there any problem with this line i am getting error console.log("There may be errors");. while filling form i make sure that there is no field left empty.
Try not to check so many fields at once. Test one first and try to console.log(errors) to see what exactly issues is.
There might be issue in client side. Do you use form submit or ajax post ? You can use postman to debug serverside first and then fix the client side. When you debug client side use chrome dev tools or similar find the post request in network and chech if you have successed attach params.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.