TypeError: validator is not a function code example
Example 1: app.use(validator()) is not a function
var router = express.Router();
const { check, validationResult } = require('express-validator');
router.post('/register',
[
check('email', 'Email is not valid').isEmail(),
check('username', 'Username field is required').not().isEmpty(),
check('password', 'Password field is required').not().isEmpty())
],
function(req, res, next) {
const errors = validationResult(req);
if (errors) {
console.log(errors);
res.render('register', { errors: errors.array() });
}
else {
console.log('No Errors');
res.render('dashboard', { message: 'Successful Registration.' });
}
});
Example 2: app.use(validator()) is not a function
route.post([check('nome', 'O nome é obrigatório').not().isEmpty(),
check('email', 'O email é obrigatório').isEmail(),
check('password', 'O password é obrigatório').not().isEmpty(),],
(req, res)=>{
const errors = validationResult(req);
if(!errors.isEmpty()){
app.utils.error.send(errors, req, res);
return false;
}
db.insert(req.body, (err, user)=>{
if(err){
app.utils.error.send(err, req, res);
}else{
res.status(200).json(user);
}
});