Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 展平/链化多个嵌套火基观测值_Firebase_Firebase Realtime Database_Observable_Rxjs5_Angularfire2 - Fatal编程技术网

Firebase 展平/链化多个嵌套火基观测值

Firebase 展平/链化多个嵌套火基观测值,firebase,firebase-realtime-database,observable,rxjs5,angularfire2,Firebase,Firebase Realtime Database,Observable,Rxjs5,Angularfire2,我正在尝试使用AngularFire2。我正在查询,下面的一切都很好 我想将所有/大部分观测值合并为一个: getTournamentWithRounds(key):Observable<Tournament> { return this.af.database .object(`/tournaments/${key}`) .map(tourney => { let t = Tournament.fromJso

我正在尝试使用AngularFire2。我正在查询,下面的一切都很好

我想将所有/大部分观测值合并为一个:

getTournamentWithRounds(key):Observable<Tournament> {

    return this.af.database
        .object(`/tournaments/${key}`)
        .map(tourney => {

            let t = Tournament.fromJson(tourney);

            this.af.database.list('players', {
                query: {
                    orderByChild: 'tournament_key',
                    equalTo: key
                }
            })
            .map(Player.fromJsonList)
            .subscribe(ps => { t.players = ps; });

            this.af.database.list('rounds', {
                query: {
                    orderByChild: 'tournament_key',
                    equalTo: key
                }
            })
            .map(Round.fromJsonList)
            .subscribe(rs => { t.rounds= rs; })

            return t;
        })
  }

您可以使用
combinelatetest
操作符将玩家和回合与锦标赛组合:

getTournamentWithRounds(key): Observable<Tournament> {

  return this.af.database
    .object(`/tournaments/${key}`)
    .combineLatest(
      this.af.database.list('players', {
        query: {
          orderByChild:'tournament_key',
          equalTo: key
        }
      }),
      this.af.database.list('rounds', {
        query: {
          orderByChild:'tournament_key',
          equalTo: key
        }
      })
    )
    .map(([tourney, players, rounds]) => {

      let t = Tournament.fromJson(tourney);
      t.players = Player.fromJsonList(players);
      t.rounds = Round.fromJsonList(rounds);
      return t;
    });
}

我已经编辑了您的问题,以分离您对问题原始代码的修改。接受答案后,您修改了问题,使答案不再正确回答问题。我认为应该避免这种情况,因为问答对任何人都没有意义。没关系。我的错。我们将避免这种情况。谢谢
getTournamentWithRounds(key): Observable<Tournament> {

  return this.af.database
    .object(`/tournaments/${key}`)
    .combineLatest(
      this.af.database.list('players', {
        query: {
          orderByChild:'tournament_key',
          equalTo: key
        }
      }),
      this.af.database.list('rounds', {
        query: {
          orderByChild:'tournament_key',
          equalTo: key
        }
      })
    )
    .map(([tourney, players, rounds]) => {

      let t = Tournament.fromJson(tourney);
      t.players = Player.fromJsonList(players);
      t.rounds = Round.fromJsonList(rounds);
      return t;
    });
}
getTournamentWithRounds(key): Observable<Tournament> {

  return this.af.database
    .object(`/tournaments/${key}`)
    .combineLatest(
      this.af.database.list('players', {
        query: {
          orderByChild:'tournament_key',
          equalTo: key
        }
      }),
      this.af.database.list('rounds', {
        query: {
          orderByChild:'tournament_key',
          equalTo: key
        }
      })
      .switchMap(rounds => {
        Observable.forkJoin(
          rounds.map(round => this.af.database.list('matches', {
            query: {
              orderByChild: 'round_key',
              equalTo: round.$key
            }
          }).first()),
          (...lists) => rounds.map((round, index) => {
            let r = Round.fromJson(round);
            r.matches = Match.fromJsonList(lists[index]);
            return r;
          })
        )
      })
    )
    .map(([tourney, players, rounds]) => {

      let t = Tournament.fromJson(tourney);
      t.players = Player.fromJsonList(players);
      t.rounds = rounds;
      return t;
    });
}