Angular 2 个 Observables - 对操作的空响应仍然显示数组中的数据

Angular 2 Observables - Empty response on action still shows data in array

我有这个功能可以从 Firebase 聚合一些用户数据以构建 "friend request" 视图。在页面加载时,会显示正确的请求数。当我点击一个 "Accept" 按钮时,正确的连接请求得到更新,然后再次向 运行 这个函数发出信号,因为用户订阅了它。唯一的问题是,一旦所有的好友请求都被接受,最后剩下的用户会留在列表中并且不会消失,即使他们已经被接受了。

这是我用来获取请求的函数:

getConnectionRequests(userId) {

    return this._af.database
      .object(`/social/user_connection_requests/${userId}`)

      // Switch to the joined observable
      .switchMap((connections) => {

        // Delete the properties that will throw errors when requesting
        // the convo keys
        delete connections['$key'];
        delete connections['$exists'];

        // Get an array of keys from the object returned from Firebase
        let connectionKeys = Object.keys(connections);

        // Iterate through the connection keys and remove
        // any that have already been accepted

        connectionKeys = connectionKeys.filter(connectionKey => {

          if(!connections[connectionKey].accepted) {
            return connectionKey;
          }
        })

        return Observable.combineLatest(
          connectionKeys.map((connectionKey => {

              return this._af.database.object(`/social/users/${connectionKey}`)
            }))
        );
      });
  }

这是我的 Angular 2 视图(使用 Ionic 2)中的相关代码:

ionViewDidLoad() {

    // Get current user (via local storage) and get their pending requests
    this.storage.get('user').then(user => {
      this._connections.getConnectionRequests(user.id).subscribe(requests => {

        this.requests = requests;
      })

    })
  }

我觉得我的 observable 做错了什么,这就是为什么会出现这个问题。任何人都可以对此有所了解吗?提前致谢!

我认为您在评论中指出了这一点。如果 connectionKeys 是一个空数组调用 Observable.combineLatest 是不合适的:

import 'rxjs/add/observable/of';

if (connectionKeys.length === 0) {
  return Observable.of([]);
}
return connectionKeyObservable.combineLatest(
  connectionKeys.map(connectionKey =>
    this._af.database.object(`/social/users/${connectionKey}`)
  )
);