Ionic / angulfire2 - 多次查询连接参考

时间:2018-03-07 09:09:28

标签: ionic2 angularfire

我正在使用Ionic和angulfire2构建应用程序,并且我尝试使用对象密钥从firebase加入多个引用。

数据库看起来如下:

{
  "achievements" : {
    "200" : {
      "authorId" : "nGSlhjaDRKh8XdrgxcusU0wdiHN2",
      "description" : "I did it"
    }
  },
  "challengeAchievements" : {
    "100" : {
      "200" : true
    }
  },
  "challenges" : {
    "100" : {
      "name" : "test challenge"
    },
    "101" : {
      "name" : "test challenge 2"
    }
  },
  "users" : {
    "nGSlhjaDRKh8XdrgxcusU0wdiHN2" : {
      "email" : "user1@test.com"
    },
    "wBMX8WOHIpM7dEkzj0hM19OPMbs1" : {
      "email" : "user2@test.com"
    }
  }
}

我想将所有这些数据加在一起,以便从挑战中获得成就,并从成就中获得用户数据。

目前我能够获得成就详情,但不能获得用户数据。我的提供商目前看起来像这样:

  getChallengeAchievements(challengeKey) {
    return this.rtdb.list(`/challengeAchievements/${challengeKey}`)
      .map(achievements => achievements.map((achievement) => {
        if (achievement.key)
          achievement.details = this.getAchievementDetails(achievement.key);

          achievement.user = this.getAchievementUserDetails(achievement.details.authorId);

          return achievement;
      }));
  }

  getAchievementDetails(achievementKey?: string): Observable<any> {
    if (achievementKey)
      return this.rtdb.object(`/achievements/${achievementKey}`);
  }

  getAchievementUserDetails(authorId?: string): Observable<any> {
    if (authorId)
      return this.rtdb.object(`/users/${authorId}`);

    else console.log('Not found');
  }

如何在此函数中构造authorId查询?如果我在

中使用静态值
achievement.details.authorId('nGSlhjaDRKh8XdrgxcusU0wdiHN2')

我能够收到数据。

1 个答案:

答案 0 :(得分:0)

通过订阅第一个加入来解决它&#34; achievement.details&#34;并从那里获取用户数据。

  getChallengeAchievements(challengeKey) {
return this.rtdb.list(`/challengeAchievements/${challengeKey}`)
  .map(achievements => achievements.map((achievement) => {

    if (achievement.key)
      achievement.details = this.getAchievementDetails(achievement.key);

      achievement.details.subscribe(
        details => {
          achievement.user = this.getAchievementUserDetails(details.authorId);
      })

      return achievement;
  }));

}