I am trying to validate user input with express validator and I get an empty array of errors which is incorrect because I sent the wrong input to test. When I console.log(errors), I get this : Result { formatter: [Function: formatter], errors: [] }
login = async (req, res) => {
try {
body("email").isEmail(), body("password").isLength({ min: 5 });
const errors = validationResult(req);
console.log(errors);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const user = await User.findOne({ where: { email: req.body.email } });
if (!user) {
return res
.status(400)
.json({ error_msg: "An account for this email does not exist" });
}
const validPass = await bcrypt.compare(req.body.password, user.password);
if (!validPass) {
return res.status(400).json({ error_msg: "E-mail or password is wrong" });
}
const token = await getSignedToken(user);
return res.status(200).json({ token: token });
} catch (error) {
return res.status(400).json({ error_msg: error.message });
}
};
This is my sample input: { "email":"samadejoro", "password":"sams" }