链接rxjs 6可观测值

链接rxjs 6可观测值,rxjs,redux-observable,Rxjs,Redux Observable,我必须创建一个ajax请求队列并对结果进行分组,但我不知道如何完成这一任务 假设我有这样一个数组: const actors = [ "Al Pacino", "Robert De Niro" ]; 我必须对其进行迭代,并对每个值进行api调用: export const getMovies = action$ => action$.pipe( ofType(LOAD_REQUEST), // iterate over the array // mak

我必须创建一个ajax请求队列并对结果进行分组,但我不知道如何完成这一任务

假设我有这样一个数组:

const actors = [
  "Al Pacino",
  "Robert De Niro"
];
我必须对其进行迭代,并对每个值进行api调用:

export const getMovies = action$ =>
  action$.pipe(
    ofType(LOAD_REQUEST),
    // iterate over the array
    // make api call with the actor name
    // for each result, make a second api call with the id of the actor (get in the response.results.id)
    // group the result in an array with all films of all actors of the array passed in the payload
  );
我被开关地图,管道卡住了。。。我不知道怎样才能做到这一点

Edit尝试了您的解决方案Valeriy,但出现以下错误:

export const getMovies = action$ =>
  action$.pipe(
    ofType(LOAD_REQUEST),
    switchMap(({ payload }) =>
      combineLatest(
        payload.map(a => {
          return ajax
            .getJSON(actor(a))
            .pipe(map(response => console.log(response)));
        })
      )
    )
  );


TypeError: You provided 'function (source) {
    return source.lift.call(Object(_observable_from__WEBPACK_IMPORTED_MODULE_2__["from"])([source].concat(observables)), new _observable_combineLatest__WEBPACK_IMPORTED_MODULE_1__["CombineLatestOperator"](project));
  }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

如果我理解正确,您正试图实现以下目标:

const actors = [
  "Al Pacino",
  "Robert De Niro"
];
export const getMovies=action$=>action$.pipe(
ofType(加载请求),
开关映射(()=>{
//将结果分组到一个数组中,该数组中所有参与者的所有胶片都传递到有效负载中
返回组合测试(
//迭代数组
…actors.map(actorName=>{
//使用参与者名称进行api调用
返回loadActor(actorName).pipe(
//对于每个结果,使用参与者的id进行第二次api调用(在response.results.id中获取)
switchMap(response=>loadActorFilms(response.results.id))
);
})
);
})
);

我曾经将多个观测值组合在一起。

如果我理解正确,那么您正试图实现以下目标:

const actors = [
  "Al Pacino",
  "Robert De Niro"
];
export const getMovies=action$=>action$.pipe(
ofType(加载请求),
开关映射(()=>{
//将结果分组到一个数组中,该数组中所有参与者的所有胶片都传递到有效负载中
返回组合测试(
//迭代数组
…actors.map(actorName=>{
//使用参与者名称进行api调用
返回loadActor(actorName).pipe(
//对于每个结果,使用参与者的id进行第二次api调用(在response.results.id中获取)
switchMap(response=>loadActorFilms(response.results.id))
);
})
);
})
);

我曾经将多个观测值组合在一起。

我尝试了你的解决方案,但出现了一个错误,我编辑了我的answer@amerej我猜这是因为
combineLatest()
需要一个可观察的数组作为输入,但是最后一个map
map(response=>console.log(response))
转换了输出类型。如果您只想记录结果,请使用而不是
map
。我尝试了您的解决方案,但出现错误,我编辑了我的answer@amerej我猜这是因为
combineLatest()
需要一个可观察的数组作为输入,但是最后一个map
map(response=>console.log(response))
转换了输出类型。如果只想记录结果,请使用而不是
map