3

订阅中的错误回调未映射到 JSON 槽映射函数!为什么?

this.http.get("/something")
            .map((response: any) => response.json())
            .subscribe((response: any) => {
                  // response is body of request
            }, (error: any) => {
                // error must be mapped again
                let error = JSON.parse(error._body);
            });
4

2 回答 2

1

因为返回的 Observableget()有错误,所以map()传递的函数没有被调用(因为没有发出事件)。即(response: any) => response.json()不调用箭头函数。相反,错误会传播,直到有东西捕获它。如果没有发现错误,您将在浏览器控制台中看到异常。

您可以使用.catch()和/或通过在subscribe().

我试图在http这里创建一个强大的错误处理程序:https
://stackoverflow.com/a/35329086/215945它捕获从.get()or生成的错误.json()

于 2016-02-19T03:56:15.957 回答
1

实际上,在map发生错误的情况下不会调用操作符指定的回调。您需要利用catch它来做到这一点:

this.http.get("/something")
        .map((response: any) => response.json())
        .catch((response: any) => Observable.throw(response.json()))
        .subscribe((response: any) => {
              // response is body of request
        }, (error: any) => {
            // error must be mapped again
            let error = JSON.parse(error._body);
        });
于 2016-02-18T18:07:29.753 回答