Angular 使用catchError运算符时角度9中的捕捉错误
嗨,我尝试使用Rxjs操作符catchError捕捉错误并发送有关日志的信息。下面我展示了我的post服务中用于获取一些数据的函数:Angular 使用catchError运算符时角度9中的捕捉错误,angular,typescript,error-handling,rxjs,Angular,Typescript,Error Handling,Rxjs,嗨,我尝试使用Rxjs操作符catchError捕捉错误并发送有关日志的信息。下面我展示了我的post服务中用于获取一些数据的函数: fetchPosts() { const url = 'some url'; return this.http.get<{ [key: string]: Post }>(url) .pipe( map((responseData) => { const postArray: P
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);
})
));
}
fetchPosts(){
const url='一些url';
返回此.http.get(url)
.烟斗(
映射((响应数据)=>{
邮递常数:邮递[]=[];
用于(响应数据中的常数键){
if(responseData.hasOwnProperty(键)){
push({…responseData[key],id:key});
}
}
返回邮资;
},catchError(errorResponse=>{
//现在我们可以将示例信息发送到分析服务
this.logService.addNewLog(errorResponse.message);
返回抛出器(errorResponse.message);
})
));
}
问题是它不能与地图操作员一起工作。当我删除贴图操作符时,它将正常工作。如下图所示:
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);
})
);
}
fetchPosts(){
const url='一些url';
返回此.http.get(url)
.pipe(catchError(errorResponse=>{
this.logService.addNewLog(errorResponse.message);
返回抛出器(errorResponse.message);
})
);
}
我怎样才能解决这个问题?我希望它能与这两个操作符一起工作:map、catchError。我的代码有什么问题?我将非常感谢您的帮助。操作员必须单独接入。此时,您正在
map
操作符内的catchError
中进行配管。试试下面的方法
fetchPosts(){
const url='一些url';
返回this.http.get(
'https://http-request-learning.firebaseio.com/posts.json'
).烟斗(
映射((响应数据)=>{
邮递常数:邮递[]=[];
用于(响应数据中的常数键){
if(responseData.hasOwnProperty(键)){
push({…responseData[key],id:key});
}
}
返回邮资;
}), // {
//现在我们可以将示例信息发送到分析服务
此.logService.addNewLog(“sss”);
返回抛出器(errorResponse.message);
})
);
}
操作员必须单独接入管道。此时,您正在map
操作符内的catchError
中进行配管。试试下面的方法
fetchPosts(){
const url='一些url';
返回this.http.get(
'https://http-request-learning.firebaseio.com/posts.json'
).烟斗(
映射((响应数据)=>{
邮递常数:邮递[]=[];
用于(响应数据中的常数键){
if(responseData.hasOwnProperty(键)){
push({…responseData[key],id:key});
}
}
返回邮资;
}), // {
//现在我们可以将示例信息发送到分析服务
此.logService.addNewLog(“sss”);
返回抛出器(errorResponse.message);
})
);
}