如何在catchError代码块中使用代码

时间:2019-02-20 03:44:13

标签: angular rxjs

我不知道正确的语法,如何在catchError中使用代码块?

this.budgetService.getBudgetDays(this.startDate, this.finishDate)
        .pipe(
          catchError(res => self.budgetTestService.getBudgetDates(self.startDate, self.finishDate))
        )
        .subscribe(res => {
          console.log('Response = ', res);
          self.timelineBudgetDates = self.processDates(res);
          //self.timelineBudgetDates = res;

        });

因此,我想使用类似的方法在其中为timelineDates分配一个值。

.pipe(
  catchError(self.timelineDates = self.budgetTestService.getBudgetDates(self.startDate, self.finishDate)
  // more code...

  )
)

3 个答案:

答案 0 :(得分:1)

Promise<T>只是一个功能

catchError

https://www.learnrxjs.io/operators/error_handling/catch.html

.pipe(
  catchError(error => {
      console.log({error});
     self.timelineDates = self.budgetTestService.getBudgetDates(self.startDate, self.finishDate)
  // more code...but return an observable
 return of('some return value, maybe null, maybe a good default');
    }
  )
)

答案 1 :(得分:0)

catchError需要一个回调函数作为参数。

.pipe(
  catchError(() => {
    self.timelineDates = self.budgetTestService.getBudgetDates(self.startDate, self.finishDate
  })
  // more code...
)

答案 2 :(得分:0)

尝试使用此

.pipe(
 catchError(err => {
  self.timelineDates = self.budgetTestService.getBudgetDates(self.startDate, self.finishDate);
  console.error(err.message);
  console.log("Error is handled");
  return throwError("Error thrown from catchError");
 })
 // more code...
)

从此处了解详细信息https://www.concretepage.com/angular/angular-catcherror