gpt4 book ai didi

angular - 使用catchError运算符捕捉Angular 9中的错误

转载 作者:行者123 更新时间:2023-12-03 08:09:00 24 4
gpt4 key购买 nike

嗨,我尝试使用Rxjs运算符catchError捕获错误并发送有关日志的信息。下面显示了我的邮政服务中用于获取一些数据的函数:

fetchPosts() {
const url = 'some url';
return this.http.get<{ [key: string]: Post }>(url)
.pipe(
map((responseData) => {
const postArray: Post[] = [];
for (const key in responseData) {
if (responseData.hasOwnProperty(key)) {
postArray.push({...responseData[key], id: key});
}
}
return postArray;
}, catchError(errorResponse => {
// now we can send for example information to analytic serv
this.logService.addNewLog(errorResponse.message);
return throwError(errorResponse.message);
})
));
}
问题在于 map 操作符无法使用。当我删除 map 运算符时,它将可以正常工作。如下所示:
fetchPosts() {
const url = 'some url';
return this.http.get<{ [key: string]: Post }>(url)
.pipe(catchError(errorResponse => {
this.logService.addNewLog(errorResponse.message);
return throwError(errorResponse.message);
})
);
}
我怎么解决这个问题?我希望它可以与这两个运算符一起使用:map,catchError。我的代码有什么问题?我将不胜感激。

最佳答案

运算符(operator)必须单独管道输送。目前,您正在管道catchError运算符内的map。尝试以下

fetchPosts() {
const url = 'some url';
return this.http.get<{ [key: string]: Post }>(
'https://http-request-learning.firebaseio.com/posts.json'
).pipe(
map((responseData) => {
const postArray: Post[] = [];
for (const key in responseData) {
if (responseData.hasOwnProperty(key)) {
postArray.push({...responseData[key], id: key});
}
}
return postArray;
}), // <-- close `map` here
catchError(errorResponse => {
// now we can send for example information to analytic serv
this.logService.addNewLog("sss");
return throwError(errorResponse.message);
})
);
}

关于angular - 使用catchError运算符捕捉Angular 9中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64622932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com