forkJoin返回并可观察



我不确定这个问题的标题是否正确,但我有以下场景。

const catalog$ = this.proxy.getCatalogData(); // this is an http req
const pay$ = this.proxy.getPaydData(); // this is an http req
const acc$ = this.proxy.getAccData(); // this is an http req
const service$ = this.proxy.getServiceData(); // this is an http req and returns some error or throwError(503)
let arr = [
catalog$,
pay$,
acc$,
service$
];
arr = arr.map(item => item.pipe(catchError(err => of(err)));
forkJoin(arr).subscribe(data => {
console.log(data); // [{...}, {...}, {...}, Observable]
});

现在,尽管我有forkJoin,但我在api的回调数据中得到了一个可观察到的抛出错误的数据。有没有办法解决或从数据数组中可观察到的错误中获取数据?所以我再也不用处理它了。

您之所以得到它,是因为您正在从管道发送一个来自catchError的可观察到的:

arr = arr.map(item => item.pipe(catchError(err => of(err)));
// --------Here-----------------------------------^^^^^^^

of()返回一个带有所提供参数的可观测值。

您可以直接返回error

arr = arr.map(item => item.pipe(catchError(err => err));

现在您可以获得错误对象。

最新更新