繁体   English   中英

从 catchError 中的 return observable 获取值

[英]Getting the value from a return observable inside catchError

budgetTestService.getBudgetDates返回一个 observable,如何在BudgetDate[]从中获取BudgetDate[]值?

this.budgetService.getBudgetDays(this.startDate, this.finishDate)
  .pipe(
    catchError(error => {
      self.timelineBudgetDates = self.budgetTestService.getBudgetDates(self.startDate, self.finishDate);
        return of('some return value');
      }
    )
  )
  .subscribe(res => {
    self.timelineBudgetDates = self.processDates(res);          
  });

错误:

Type 'Observable<BudgetDate[]>' is missing the following properties from type 'BudgetDate[]': length, pop, push, concat, and 25 more.

您可以使用高阶函数将此引用传递给 catch

const errorHandler=(self)=>error => {
              self.timelineBudgetDates = 
              self.budgetTestService.getBudgetDates(self.startDate, self.finishDate);
              return of('some return value');
            }
          )

this.budgetService.getBudgetDays(this.startDate, this.finishDate)
        .pipe(
          catchError(errorHandle(this))
        )
        .subscribe(res => {
          self.timelineBudgetDates = self.processDates(res);

        });

如果您从组件调用 budgetService,您应该能够在无需订阅的情况下映射响应。 catchError 仅在服务抛出错误时执行。 请看下面的代码...

    this.budgetService.getBudgetDays(this.startDate, this.finishDate)
        .pipe(
          map(res => { self.timelineBudgetDates = self.processDates(res); }), 
          catchError(error => {
              // handle error
            })
         );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM