我正在使用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')
我能够接收数据。
通过订阅第一个连接"achievement.details"并从那里获取用户数据来解决它。
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;
}));
}