Here is my code:
try {
const user = await User.findOne({ email: req.body.email });
!user && res.status(404).json("user not found");
const validPassword = await bcrypt.compare(req.body.password, user.password)
!validPassword && res.status(400).json("wrong password")
res.status(200).json(user)
} catch (err) {
res.status(500).json(err)
}
});
If I use the right credentials there is no problem but if I type in wrong password or email I get an error stating:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
and the app crashes, im following a YouTube tutorial by Lama Dev on Node.js Social media API and have the code copied one by one.