【问题标题】:`withLatestFrom` - throw error on implementing with observable`withLatestFrom` - 使用 observable 实现时抛出错误
【发布时间】:2019-05-08 10:20:10
【问题描述】:

我正在尝试使用 rxjs withLatestFrom 运算符集成 ngrx 缓存。但是得到一个错误:

You provided 'undefined' where a stream was expected.
You can provide an Observable, Promise, Array, or Iterable.

有人更正我的代码吗?

这是我的代码:

@Effect()
EffLoadCourse = this.actions.pipe(
  ofType(LoadCourse.TYPE),
  withLatestFrom(
    this.store.pipe(select(subscribes.getCourses)), //getting it from store
    (action: LoadCourse, courses: ModelCourse[]) => {
      // console.log('action is ', action, 'courses is', courses)
      return courses;
    }
  ),
  mergeMap((courses: ModelCourse[]) => {

    if (courses) { //if courses available just return it
      map((courses: ModelCourse[]) => (new LoadCourseSuccess(courses))),
        catchError(err => of(new LoadCourseFail(err)))
      return;
    }
    //only return on courses unavailability
    return this.courseService.getCourse().pipe(
      map((courses: ModelCourse[]) => (new LoadCourseSuccess(courses))),
      catchError(err => of(new LoadCourseFail(err)))
    )
  })
)

【问题讨论】:

  • 请同时显示您的选择器“getCourses”以及您如何定义您的状态
  • mergeMap!courses 时您没有返回任何内容

标签: rxjs ngrx rxjs-pipeable-operators


【解决方案1】:
   if(courses){ //if courses available just return it
                    map((courses:ModelCourse[]) => (new LoadCourseSuccess(courses))),
                    catchError(err => of(new LoadCourseFail(err)))
                    return;
                }

你没有返回一个动作,只是一个void

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    相关资源
    最近更新 更多