Reputation: 437
I'm writing a chat application using MEAN stack. where I'm validating registration screen with express-validator package. Below is the source code of package inclusion in my server.js file. where I created a server.
let express = require('express');
let application = express();
let path = require('path');
let db = require("./db");
const server = require('http').createServer(application);
let bodyParser = require('body-parser');
let expressValidator = require('express-validator')
When a user clicks on register button. I will redirect the user to a registation controller where i'm having below piece of code. `module.exports.RegisterUser = (req, res) => { if (req.body) { let user = new userModels(req.body);
req.check('username', 'Not a valid username').isEmail();
req.check('password', 'password doen\'t meet criteria').isAlpha();
var validationErrors = req.ValidationErrors();
if(validationErrors){
console.log(validationErrors.msg);
}
// if there is no validation error
user.save()
.then(user => {
return res.status(HttpStatus.CREATED).send("Sign up successfull");
})
.catch(err => {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).send(err);
});
} else {
res.status(HttpStatus.BAD_REQUEST).send("Invalid Input");
}
}`
I can able to build the project but wasn't able to validate. I'm getting req.ValidationErrors() is not a function.
thanks in advance.
Upvotes: -1
Views: 2118
Reputation: 9
Try this:-
const { validationResult } = require('express-validator');
//In function body
const errors = validationResult(req);
if (!errors.isEmpty()) {
//Rest logic
}
Upvotes: -1
Reputation: 145
I believe it needs to be:
const validationErrors = req.validationErrors();
instead
EDIT: Whoops, just noticed Sterling Archer already posted with correct answer. Either way, I'll leave this here as reminder for all of us that don't read well
Upvotes: 1