从catchError内部可观察到的返回值获取值

时间:2019-02-20 05:57:28

标签: angular rxjs

budgetTestService.getBudgetDates返回一个可观察值,如何在catchError中从中获取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.

2 个答案:

答案 0 :(得分:0)

如果要从某个组件调用budgetService,则应该能够映射响应而无需订阅。仅在服务引发错误时才执行catchError。请参见下面的代码...

import data_utils
import seq2seq_model
import translate
import tensorflow as tf

with tf.Session() as sess:    
    saver = tf.train.import_meta_graph('/home/i9/L-T_Model_Training/16_NOV_MODEL/train/translate.ckpt-615700.meta')
    saver.restore(sess,tf.train.latest_checkpoint('/home/i9/L-T_Model_Training/16_NOV_MODEL/train/.'))

答案 1 :(得分:0)

您可以利用高阶函数将该引用传递给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);

        });