2

我有一个从 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()
);

我试图将这些数据作为一个可观察的数据返回,但是虽然我可以看到填充正确数据库引用的路径,但什么时候说完我的配置数组只包含几千个“未定义”条目我做错了什么?我尝试了各种方法,但结果总是一样的,所以我将问题浓缩到下面并希望得到一些帮助

//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();
4

1 回答 1

0

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

如果我理解正确,您想要获取一个 itemId 数组,为每个项目进行数据库调用,然后返回一个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上的示例

于 2020-08-18T03:17:31.593 回答