首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接rxjs 6可观测数据

链接rxjs 6可观测数据
EN

Stack Overflow用户
提问于 2019-11-26 19:12:38
回答 1查看 357关注 0票数 1

我必须创建一个ajax请求队列,并对结果进行分组,但我不知道如何做到这一点。

假设我有这样一个数组:

代码语言:javascript
复制
const actors = [
  "Al Pacino",
  "Robert De Niro"
];

我必须对其进行迭代,并对每个值进行api调用:

代码语言:javascript
复制
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
  );

我被switchMap卡住了烟斗..。也不知道怎样才能做到这一点。

编辑尝试了解决方案Valeriy,但得到了以下错误:

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-26 20:05:11

如果我对你的理解是正确的,你就是在试图实现这样的目标:

代码语言:javascript
复制
export const getMovies = action$ => action$.pipe(
    ofType(LOAD_REQUEST),
    switchMap(() => {
        // group the result in an array with all films of all actors of the array passed in the payload
        return combineLatest(
            // iterate over the array
            ...actors.map(actorName => {
                // make api call with the actor name
                return loadActor(actorName).pipe(
                    // for each result, make a second api call with the id of the actor (get in the response.results.id)
                    switchMap(response => loadActorFilms(response.results.id))
                );
            })
        );
    })
);

我使用combineLatest将多个可观察到的数据组合在一起。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59057913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档