import {check, validationResult} from 'express-validator';
export const validate = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
throw new Error(errors.array());
}
next();
};
router.post(
'/register',
[
check('first_name').exists(),
check('last_name').exists(),
check('username').exists(),
],
validate,
(req, res) => {
return res.json({status: 'success', message: 'Ok'});
},
);
express validator return 500 [object Object] while console does correct print
[
{
value: undefined,
msg: 'Invalid value',
param: 'last_name',
location: 'body'
},
{
value: undefined,
msg: 'Invalid value',
param: 'username',
location: 'body'
}
]
How can we fix this please guide it should print what it is receiving into console
Please update your middleware with response or create error middleware
export const validate = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.json({error: errors.array()})
}
next();
};
Try this it should work