3

I have this axios code and I can't reach response status code. What is wrong here? I get 'undefined' instead of 201, for example.

Thanks in advance!

  axios.post('endpoint', { body })
    .then((response) => {
        console.log(response.status);
    })
2
  • 1
    Give a minimal reproducible example; what's Api? That should work with Axios, unless you've configured it somewhere. Commented Apr 7, 2020 at 22:10
  • @jonrsharpe sorry. edited Commented Apr 7, 2020 at 22:16

2 Answers 2

3

In case of error you have to catch the result

axios.post('endpoint', { body })
    .then((response) => {
        // do something
    })
    .catch((error) => {
        console.log(error.response.status)
    })

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

3 Comments

i get error code 500 and in this case code inside .then is running
In this case response.status should work. Can you share the response object?
For me the response is just "false" in case of a 404 response
0
    post(endpoint, body) {
        return axios.post(endpoint, body)
          .then((response) => {
            return { 
               error: null, 
               data: response.data, 
               status: response.status 
             };
          })
          .catch((error) => {
            return { error: error };
      });
  }

Comments

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.