0

I have made an Api with Node.js but I have some weird behaviour with my error handling. You can see the code below. I'm checking a form with node.js if the form is not valid I'm sending back to the frontend an error message. I can get it(error 400 and 409) from the frontend on error.response.data.message(catch).

After I'm making an API call with axios if we cannot find the information sent by the user I return an error on the same way than before, but that time on the frontend I get it on error.response.data.response.data.message(the error 404).

So to receive it on error.response.data.message, I have to send the error like that this time: error.message.

So it is working but I just would like to understand what happens exactly.

Thanks in advance for your help!!!!!!

let error={response:{data:{message:"Thanks to fill properly all 
fields!!"}}}
if (formValid.length>0){  res.status(400).send(error);}
else{ 

    axios.get('https://someapi',{headers: 
    {
    "Authorization" : "XXXXXXX"
    }
    })
    .then(response => {
    var newUser = new User({username:req.body.username....});
    User.register(newUser,req.body.password, function(err, user){
    if(err){
       res.status(409).send(err);
    }
    else{ 
    res.status(200).send();
    }  

    })
    }).catch(e => {
    error={response:{data:{message:"The information you sent seem 
    wrong please check it again...}}}

    res.status(404).send(error);
    })}
}
2
  • "it is working but I just would like to understand what happens exactly" -- this is too broad and not a specific programming problem. Commented Apr 30, 2019 at 2:28
  • So why I have to send 2 different objects to get the same thing at the arrival (In my example I'm sending two equivalent objects but I get my message at different path? When I do something I would like to know exactly how it works. Commented Apr 30, 2019 at 8:28

1 Answer 1

1

Exactly if you observe the above error by logging it to the console you will see that it contains the message property in it direcly and that message property is binded to the data property of response but in below case you are passing the nested javascript object to error property which again adds the response property in error object so you need to access that message through error.response.data. response..... So in this scenario the whole javascript object is added to data property which causes that result.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for taking the time to answer my question. Sorry if I was not clear. The issue is when I send the error 409 above of my code it is working as expected. Just a few lines below if I send exactly the same object I get it on error.response.data.response.data.message whereas the error above is received on error.response.data.message?
Exactly if you observe the above error by logging it to the console you will see that it contains the message property in it direcly and that message property is binded to the data property of response but in below case you are passing the nested javascript object to error property which again adds the response property in error object so you need to access that message through error.response.data. response..... So in this scenario the whole javascript object is added to data property which causes that result.
Just replace this error={Message:'Your error message'} and send it using res.send(error) method.
"if you observe the above error by logging it to the console you will see that it contains the message property in it directly and that message property is binded to the data property of response but in below case you are passing the nested javascript object to error property which again adds the response property in error object" It is just what I was looking for a clear explanation! Thanks, Pranil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.