Angularfire2列表查询limitToLast

时间:2018-02-11 02:22:11

标签: angular typescript firebase firebase-realtime-database angularfire2

我想访问firebase数据库中的特定节点。

enter image description here

我实际上是以:

运行
  • “angularfire2”:“^ 5.0.0-rc.4”,
  • “firebase”:“^ 4.9.0”,

我的组件中的代码如下所示:

  this.id = this.route.snapshot.paramMap.get('id'); 
  auth.appUser$.subscribe(appUser => this.appUser = appUser );  

  if (this.id) 
  {
      //Live game state info          
      this.subscription = this.gameService.getLive(this.id).subscribe(action => {
        this.game = action.payload.val(); 
        console.log(this.game);
      });
    }
}

我在gameService中调用的函数如下所示:

 getLive(gameId) 
  {
    return this.db.object('/games/live/' + gameId).snapshotChanges();
  }

问题是我不知道如何在轮次中访问节点“StartedAt”。但是我可以从我的实际“游戏”中看到所有完整的节点:

enter image description here

如何从特定回合中访问“startedAt”?

2 个答案:

答案 0 :(得分:0)

game.rounds[roundId].startedAt

答案 1 :(得分:0)

组件:

      this.subscription = this.gameService
      .getLive(this.id)
      .subscribe(action => {
        this.game = {key: action.key, value: action.payload.val()};
        this.game.value.playerList = Object.values(this.game.value.playerList);
  

this.game.value.rounds = Object.values(this.game.value.rounds);

        console.log(this.game.value.rounds[this.game.value.rounds.length-1]);
      });
    }

游戏模型:

import { Round } from './round';
import { Player } from './player';

export interface Game {
 key: string,
 value:{
 state: string, 
 playerList: Player[], 
 rounds: Round[], 
 startedAt: string,
 roundCounter: string,
       }
}