1

I have a code fragment like this:

(...).catch(e) => {
         if(e.response.status === 401) {
          console.log("Wrong username or password")
       } else {
           console.log(e.statustext)
}}

And I have error like this: Unhandled Exception (TypeError): cannot read property status of undefined

How can I fix it?

1
  • 2
    can you console.log(e) and see what it is, presumably response doesn't exist on the e object Commented Jun 8, 2020 at 15:34

1 Answer 1

1

catch block is passed an Error object and it doesn't contains any property named response.

Inside the then() block, check if status code is 401, if it is, throw an Error with the message "Wrong username or password" and inside the catch block, log the message using Error.prototype.message

.then(response => {
    if (response.status === 401) {
       throw new Error("Wrong username or password");
    }
    ....
 })
 .catch(e) => console.log(e.message));
Sign up to request clarification or add additional context in comments.

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.