简体   繁体   English

从 catchError 中的 return observable 获取值

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

budgetTestService.getBudgetDates returns an observable, how can I get the BudgetDate[] value from it in 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);          
  });

Error:错误:

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

You can make use of Higher order function to pass this reference to catch您可以使用高阶函数将此引用传递给 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);

        });

If you are calling the budgetService from a component, you should be able to map the response without the need to subscribe.如果您从组件调用 budgetService,您应该能够在无需订阅的情况下映射响应。 catchError is only execututed when the service throws an error. catchError 仅在服务抛出错误时执行。 Please see code below...请看下面的代码...

    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