my code is; `
router.post("/login", async (req, res)=>{
try {
const user = await User.findOne({ email: req.body.email });
!user && res.status(401).json("Wrong password or username !");
const bytes = CryptoJS.AES.decrypt(user.password, process.env.SECRET_KEY);
const originalPassword= bytes.toString(CryptoJS.enc.Utf8);
originalPassword !== req.body.password && res.status(401).json('Wrong Password or Username! --this code ');
const {password, ...info} = user._doc;
res.status(200).json(info)
} catch (err) {
res.status(500).json(err);
}`
I have shared the error and postman request as a screenshot. There is no problem when status returns 200 but when 200 is not I get this error.
How can ı solve?
2
Answers
I solved like this!
Thank you!
Firstly; Errors in Node.js are handled through exceptions. An exception is created using the
throw
keyword.See here for more information. Error handling in Node.js
Your mistake is here:
The
res.json()
function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using theJSON.stringify()
method.