在 Angular/RxJS 中尝试失败后如何重新提交可观察的操作流?

问题描述 投票:0回答:0

请忽略变量名称和格式,因为我刚刚更改了它们。

我一直在尝试为采取操作(用户点击)的可观察对象实现 RxJS 错误处理,然后从我们的表单发送请求对象并将其传递给 HTTP 调用,如下所示。

问题:如果我在我们的网页上发送一个后端不存在的请求(从一个表单),它会失败并且我得到一个错误返回(太棒了!),但是如果我在我失败后尝试重新提交正确的请求尝试,应用程序只是挂起并加载并且不执行任何操作。

serivce.ts
文件

sendRequest$ = this.exampleRatingSubjectAction$.pipe(switchMap(exampleRequest => this.http.post<exampleresponse>(this.PCFUrl + '/example',exampleRequest).pipe(
  catchError(this.handleError)
 )),
 tap(data => console.log(data)),
 );

sendExampleRequestEconomy$ = this.exampleSubjectActionEconomy$.pipe(switchMap(exampleRequest=>  this.http.post<exampleresponse>(this.PCFUrl + '/example',exampleRequest)
 .pipe(
  catchError(this.handleError)
 )),
 tap(data => console.log(data)),
 );

private handleError(err: HttpErrorResponse): Observable<never> {
// in a real world app, we may send the server to some remote logging infrastructure
//instead of just logging it to the console
let errorMessage: string;
if (err.error instanceof ErrorEvent) {
// A client-side or network error occured. Handle it accordingly
errorMessage = `An error occured: ${err.error.message}`;
} else {
// The backend returned an unsuccessful response code.
// the resposne body may contain clues as to what went wrong,
errorMessage = `Backend returned code ${err.status}: ${err.message}`;
}
console.error(err);
return throwError(errorMessage);
}

component code:

exampleResponsePriority$ = this.dataService.sendExampleRequest$
.pipe(
   map(data => this.exampleResponsePriority = data),
   tap(data => { 
    this.showPageLoader = false; 
    this.calculatorService.checkMinOrDeficitRate(data);
}),
catchError( err => {
   this.showPageLoader = false;
   // await this.presentAlert(err);
   this.errorMessageSubject.next(err);
   console.log("priority")
   return EMPTY;
}));

exampleResponseEconomy$ = this.dataService.sendExampleRequestEconomy$
.pipe(
  map(data => this.exampleResponseEconomy = data),
  catchError( err => {
    this.showPageLoader = false;
    // await this.presentAlert(err);
    this.errorMessageSubject.next(err);
    console.log("economy")
   return EMPTY;
}));

我唯一的猜测是我们的操作流已经在第一次点击时用请求对象更新,然后当后端的响应是 500 时,它可能卡在上面并且不允许重新提交?

我试过根据失败的请求更新操作流,但无法正常工作。

我觉得我只差几行代码了!

任何帮助都会很棒。

angular typescript rxjs rxjs6 rxjs-observables
© www.soinside.com 2019 - 2024. All rights reserved.