0

I am always getting an error

ERROR Error: Uncaught (in promise): e: {"headers":{"normalize dNames":{},"lazyUpdate":null},"status":400,"statusText":"Bad Request"

code is: (updated)

async verify() {
  let schema = {"token": localStorage.getItem('id_token')};
  let response: any = await this.http.post('/api-token-verify/', schema).toPromise();
  return response;
}

public async tokenAliveState() {
  console.log("1");
  let stuff = await this.verify();
  console.log("2");

  console.log("Returning from tokenAliveState");
  return tokenNotExpired('id_token');
}

All I want to do is make a post and wait for the response before moving on. The response is plain JSON.

Angular core 4.

Any ideas? Any more info needed?

Does anyone have a working example of a simple function to wait on an http response before continuing execution? All the example I found online show similar to this but no matter what I do I get the uncaught exception.

3
  • 1
    You're not doing the request as the API is expecting it. It's just that. That's what 400 means. Commented Oct 18, 2018 at 20:34
  • Is the url /api-token-verify/ correct? does schema contain the proper data? Just saying... some questions to point you in the right direction... Commented Oct 18, 2018 at 20:35
  • You awaiting function inside other no-asynchronus function? Commented Oct 18, 2018 at 20:45

2 Answers 2

1

You should always wrap an await call in a try catch block and then handle it accordingly.

try {
    let response: any = await this.http.post('/api-token-verify/', schema).toPromise();
} catch (err) {
    // Handle the 400 http code here.
}
Sign up to request clarification or add additional context in comments.

Comments

0

"stuff" is a promise. You need to .then() and .catch() the response/errors from the async call. Basically, your http call didn't go through and you did not catch the error returned from the promise.

stuff
  .then(() => {})
  .catch((error) => {}) 

should make your error go away.

3 Comments

BTW, tokenAliveState must be sync too
Tried this. Same error as original. 400 is ok from that api, I just don't want it to crash. That is the built in django rest framework jwt verify api. It returns 400 if the token is bad and 200 if it is good
@MichaelaErvin If you catch your errors, it won't crash or say "uncaught in promise"

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.