skip to Main Content

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


  1. Chosen as BEST ANSWER
    if (req.body.password !== originalPassword) {
      res.status(401).json("Wrong password or username !");
      return;
    }
    

    I solved like this!

    Thank you!


  2. 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:

    res.status(401).json({error: "Wrong Password or Username! --this code" });
    

    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 the JSON.stringify() method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search