I tried to test my login request in postman with an empty body but this check doesn’t work. It goes beyond and execute the rest of the code. Why ?
Is there another way to check if body is empty ?
route.post("/login", (req, res) => {
console.log(req.body);
if (!req.body) {
console.log("I am here");
res.status(400).send({ message: "Content cannot be empty" });
return;
}
... // check password etc
}
>Solution :
You can check how many keys req.body has
if (Objects.keys(req.body).length === 0) {
console.log("I am here");
res.status(400).send({ message: "Content cannot be empty" });
return;
}