如何合并来自引用数组的角射

问题描述

我有一个从angularfire检索的引用数组

this.userIds$ = this.users$.switchMap(email =>
  db.list('/USERS/',ref => {
    let temp = email ? ref.orderByChild('EMAIL').startAt(email).endAt(email + "\uf8ff") : ref
    console.log('carrot');
    return temp;
  }
  ).snapshotChanges()
);

我试图以可观察的方式返回此数据,但是尽管我可以看到路径填充了正确的数据库引用,但说完了之后,我的configs数组仅包含数千个“未定义”条目,我在做什么错误?我尝试了各种方法,但结果始终是相同的,因此我将问题压缩到下面,并希望寻求帮助

//Subscribe to the output of user ids,when we get a hit (delivered as an array of Observable DataSnapshot) loop through and subscribe to all the children of this location
    this.userConfigs$ = this.userIds$.pipe(
      switchMap(itemIds => {
        let configs = itemIds.map(itemId => {
          let pathOrRef = '/AIS/USERSAVEDCONFIGS/' + itemId.key;
          console.log('tomato');
          db.list(pathOrRef).snapshotChanges(['child_added']);
        });
        return configs;
      })
    );

然后我将整个事情拉开序幕

this.userConfigs$.subscribe();

解决方法

您传递给switchMap的函数应该返回一个Observable,而您正在返回一个数组。我认为TypeScript编译器应该对此发出警告。

如果我的理解正确,您想获取一个itemIds数组,对每个项目进行数据库调用,然后返回一个pathOrRef数组。

如果是这样,应该可以执行以下操作:

  1. 使用from()一次发射一次userIds
  2. 使用map()将项目转换为pathOrRef
  3. 使用switchMap()进行数据库调用
  4. 使用mapTo()返回pathOrRef
  5. 使用toArray()将排放物排成一列
this.userConfigs$ = this.userIds$.pipe(
  switchMap(itemIds => from(itemIds).pipe(
    map(itemId => `/AIS/USERSAVEDCONFIGS/${itemId}`),switchMap(pathOrRef => fakeDatabaseCall(pathOrRef).pipe(mapTo(pathOrRef)))
  )),toArray()
);

以下是StackBlitz

上的示例

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...