【问题标题】:Angular 2 - Observables returning twice but after a few seconds...not sure whyAngular 2 - Observables 返回两次但几秒钟后......不知道为什么
【发布时间】:2017-01-20 18:44:41
【问题描述】:

我有一个 Ionic 2 应用程序,它的功能是通过 observables 聚合一堆数据。数据回来了……除了第一次返回时它并不总是完整的。几秒钟后,我的数据再次返回,除了两个实例现在都填充了完整的数据。这真的很奇怪,我不确定是什么原因造成的。

这是我的功能:

getUserStories(data) {
    return this._af.database
      .object(`/social/users/${data.id}`)

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

        let connections = [];
        let connectionKeys = Object.keys(user.connections);

        return Observable.combineLatest(
          connectionKeys.map((connectionKey) => this._af.database
            .object(`/social/users/${connectionKey}`)
          ),
          (...connections) => {

            connectionKeys.forEach((connectionKey, index) => {

              this._af.database
                .object(`/social/user_stories_seen/${connectionKey}/${data.id}`).subscribe(data => {

                // Iterate over the connections and append the correct "last_seen" variable
                connections.forEach((connection, index) => {
                  if(connection.$key === connectionKey) {
                    connections[index]['last_seen'] = data;
                  }

                })
              });
            })
            return connections;
          });
      })
   }

这是调用此功能的视图:

ionViewDidLoad() {

    // Get the user from storage and get all the connections
    this.storage.get('user').then(data => {

      //Get the user profile
      this._users.getUserStories({id: data.id}).subscribe(stories => {
        console.log('stories', stories);
      });
    })
}

还有其他人遇到过这个问题吗?

【问题讨论】:

    标签: angular firebase firebase-realtime-database ionic2


    【解决方案1】:

    问题是您有内部可观察对象正在订阅然后更新连接的last_seen 属性。

    那些 observables 没有被组合到你返回的 observable 中,所以它们可以在你的combineLatest 操作符发出之后发出和更新连接。

    可以通过使用结合连接和上次看到的值的函数来简化实现:

    getUserStories(data) {
    
      return this._af.database
        .object(`/social/users/${data.id}`)
        .switchMap((user) => {
    
          const getConnection = (connectionKey) => {
            return Observable.combineLatest(
    
              // Combine the connection and last-seen value:
    
              this._af.database
                .object(`/social/users/${connectionKey}`),
              this._af.database
                .object(`/social/user_stories_seen/${connectionKey}/${data.id}`),
    
              // Assign the last-seen value, but return the connection:
    
              (connection, seen) => {
                connection.last_seen = seen;
                return connection;
              }
            );
          };
    
          const connections = Object.keys(user.connections).map(getConnection);
          return Observable.combineLatest(...connections);
        });
    }
    

    【讨论】:

    • 有趣,由于某种原因,当我运行这个函数时,我得到一个运行时错误“data.splice is not a function”:TypeError: data.splice is not a function at localhost:8100/build/main.js:33596:34在 Array.forEach(本机)
    • 是的,您所做的修复对于解决“this”很有帮助,但我遇到了 data.splice 问题。我正在使用 RXJS 5.0.0-beta.12
    • 哦,快点,如果我只是更新 rxjs 会更好吗?
    • 所以实际上,我对这里需要做什么感到有点困惑。你是说放弃你的答案并调整我当前的功能吗?
    • 伙计,我很欣赏这个答案,但不幸的是,它仍然给我 data.splice 问题:/ 当我尝试更新到最新的 v5.0.3 rxjs 时,我收到各种警告和一个来自 npm 的错误
    猜你喜欢
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2020-04-28
    • 1970-01-01
    相关资源
    最近更新 更多