【问题标题】:Angular promise won't catch exceptionsAngular Promise 不会捕获异常
【发布时间】:2018-01-14 11:44:56
【问题描述】:


我有一个登录功能,我想捕获服务器启动的异常并将它们冒泡到带有错误消息的视图中。到目前为止,我使用promise 处理异步部分,并且我想使用catch() 部分来捕获我的异常。问题是 handler 函数永远不会被调用,我在浏览器控制台中得到错误。到目前为止,这是我的代码,非常基本:

login(username: string, password: string): Promise<User> {

    return this.http
      .get(this.loginUrl + '?username=' + username + '&password=' + password)
      .toPromise()
      .then(response => response.json())
      .catch(function(e){
          this.handler(e);
      })
  }

  handler (e: Response){
    throw e;
  }

我错过了什么吗?
谢谢。

编辑 我按照建议使用了arrow function,现在我的代码如下所示:

user.service.ts

login(username: string, password: string): Promise<User> {

    return this.http
      .get(this.loginUrl + '?username=' + username + '&password=' + password)
      .toPromise()
      .then(response => response.json())
      .catch(e => {
          this.handler;
      })
  }

  handler (e: Response){
    throw e;

  }

login.component.ts

onSubmit(loginForm: NgForm): string {

    this.userService.login(loginForm.value.username, loginForm.value.password)
    .then(data => {

      this.message = 'Nome: ' + data.nome +
                    ' Cognome: ' +data.cognome + 
                    ' Azienda: ' + data.azienda +
                    ' Ufficio: ' + data.ufficio;
    })
    .catch(e => {
        this.message = e;
      });

    return this.message;
  }

尽管如此,我还是从服务器获得了异常,但应用程序跳过了 catch() 块。

【问题讨论】:

  • 控制台中的异常是什么?
  • 你又在处理程序中抛出异常?此外,如果您想使用this,您应该使用arrow function 而不是匿名函数
  • @Maximus 是 401 Unauthorized,但我认为这不是问题。
  • @0mpurdy 我是,但箭头函数是关键点。我将编辑我的代码以进一步参考该解决方案。如果您将您的评论作为答案,我会选择它作为正确的。谢谢。
  • @esseara 看起来已经有这个效果的答案,可以接受:) 另外,我不确定,但最好不要在你的问题中包含答案- 它可能会混淆未来的访问者:)

标签: javascript angular typescript exception promise


【解决方案1】:

这不是您应该使用箭头函数的方式,也不是您处理async 请求的方式。首先,您确实不应该使用function 关键字,而是使用箭头符号。但是在您的 catch 语句中,您没有启动 handler 函数。

另一方面,您试图在onSubmit 中返回一个字符串。但是获取这个字符串的方法是async。为此,您可以利用 TypeScript awaitasync 功能。

user.service.ts

login(username: string, password: string): Promise<User> {

    return this.http
      .get(this.loginUrl + '?username=' + username + '&password=' + password)
      .toPromise()
      .then(response => response.json())
      .catch(this.handler)
}

handler = (e: Response) => {
    throw e;
};

注意箭头函数赋值。

login.component.ts

async onSubmit(loginForm: NgForm): Promise<string> {

    try {
       const data: any = await this.userService.login(loginForm.value.username, loginForm.value.password);
       this.message = `Nome: ${data.nome} Cognome: ${data.cognome} Azienda: ${data.azienda} Ufficio: ${data.ufficio}`;
    } catch (e) {
       this.message = e;
    }    

    return this.message;
}

请注意,您的 onSubmit 现在返回 Promise&lt;string&gt;

【讨论】:

  • 您好,感谢您的建议。您能解释一下为什么我的异步调用解决方案不正确吗?
  • 当它到达return this.message时,这还没有设置,因为this.userService.login方法是异步的。这意味着当方法已经返回时它将到达.then
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 2015-03-16
  • 2010-10-14
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多