我继承了一个很大的Angular应用程序,必须将其从5.2升级到7.2。我设法做到了,但是有一个我无法解决的错误。
//编辑
我找到了一个解决方案,但我不知道它是否有效。
processReportResponse(response: HttpResponse<any>) {
if (response.status === 204) {
throw new HttpErrorResponse({ error: 'NoDataError' });
} else {
return new Blob([response.body], { type: 'application/pdf' });
}
}
//结束编辑
我们正在基于某些输入生成报告。单击生成时,将调用createdSalesByDateReport
函数,如下所示:
createSalesByDateReport() {
const fromTemp = new Date(this.from);
let toTemp = new Date(this.to);
if (this.selectMonth) {
// select the whole month of the from date
fromTemp.setDate(1);
toTemp = new Date(fromTemp.getFullYear(), fromTemp.getMonth() + 1, 0);
toTemp.setHours(23, 59, 59, 999);
// setting day to 0 means one day less than first day of the month
}
this.startLoading();
if (!this.userSelectionDisabled && this.selectedUserIds && this.selectedUserIds.length > 0) {
this.createUserSalesReport(fromTemp, toTemp, this.selectedUserIds);
} else {
this.createOrganizationSalesReport(fromTemp, toTemp, this.selectedUserGroupIds);
}
}
由于我们正在创建用户销售报告,因此总是会调用createUserSalesReport()
:
createUserSalesReport(from: Date, to: Date, userIds: string[]) {
this.reportService.getOrganizationUsersSalesReport(from, to, userIds)
.subscribe(
reportResponse => this.userGroupsSalesResponse(reportResponse, from, to),
res => this.processServerException(res)
);
}
这将调用报告服务的getOrganizationUsersSalesReport()
函数:
getOrganizationUsersSalesReport(from: Date, to: Date, userIds: string[]): Observable<any> {
return this.http.get(this.url + 'userSales?from=' + from.getTime() +
'&to=' + to.getTime() + '&timezone=' + Intl.DateTimeFormat().resolvedOptions().timeZone + '&userIds='
+ userIds.map(function (userId) { return userId; }),
{ observe: 'response', responseType: 'blob' })
.pipe(map(res => {
this.processReportResponse(res);
}));
}
这转到服务的processReportResponse()
函数:
processReportResponse(response: any) {
if (response.status === 204) {
throw throwError('NoDataError');
} else {
return new Blob([response.body], { type: 'application/pdf' });
}
}
由于我们在组件的createUserSalesReport()
函数中订阅了响应(如上所示),它要么是error要么是Blob。
在我的情况下,应该弹出一个带有给定错误的通知,因为该服务的processReportResponse()
的状态为204的HttpResponse
,这会将错误传递给组件的createUserSalesReport()
函数,因此processServerException()
函数被调用:
processServerException(res: any) {
console.log(res);
this.stopLoading();
if (res.error && res.error === 'NoDataError') {
this.alertService.info(this.translateService.instant('reporting.nodata'));
}
}
但是不幸的是,响应的格式我无法检查或读取,因此条件不会将响应的内容传递给alertService
这是回复的格式:
当我可以检查错误的内容并且processServerException()
中的条件可以将其传递给alertService
时,我希望以这种格式获得错误响应。
答案 0 :(得分:1)
您尝试过这个吗?
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
processReportResponse(response: HttpResponse<any>) {
if (response.status === 204) {
throw throwError({error: 'NoDataError'});
} else {
return new Blob([response.body], { type: 'application/pdf' });
}}
答案 1 :(得分:0)
您需要展平结果,以从第二个请求中返回可观察到的结果。现在,您将返回一个可观察对象。为了使结果平坦,我们可以使用switchMap
:
import { switchMap } from 'rxjs/operators'
// ...
return this.http.get(...)
.pipe(
switchMap(res => { // change map to switchmap!
return this.processReportResponse(res);
})
);