Angular 4 异步等待承诺中未捕获的异常



>我总是收到错误

错误

错误: 未捕获 (在承诺中(: e: {"标头":{"规范化 dNames":{},"lazyUpdate":null},"status":400,"statusText":"Bad Request">

代码为:(更新(

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');
}

我想做的只是发一个帖子并等待回复,然后再继续。 响应是普通的 JSON。

角芯 4.

有什么想法吗? 需要更多信息吗?

有没有人有一个简单的函数在继续执行之前等待 http 响应的工作示例?我在网上找到的所有示例都与此类似,但无论我做什么,我都会得到未捕获的异常。

您应该始终将等待调用包装在 try catch 块中,然后相应地处理它。

try {
let response: any = await this.http.post('/api-token-verify/', schema).toPromise();
} catch (err) {
// Handle the 400 http code here.
}
">

stuff"是一个承诺。你需要 .then(( 和 .catch(( 来自异步调用的响应/错误。基本上,您的http调用没有通过,并且您没有捕获从promise返回的错误。

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

应该让你的错误消失。

最新更新