2017-06-12 45 views
0

我试图从Typescript中使用AngularFire2从Firebase读取数据。我的代码在检索数据列表时与异步管道协同工作,但在获取对象时,我无法访问这些内容。上火力地堡AngularFire2检索对象数据不工作,但列表数据确实

数据结构:(myapp.firebaseio.com/LeagueStandings) Firebase Console showing data

子集的代码:

import { Component } from '@angular/core'; 
import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database'; 

Standings: FirebaseObjectObservable<any[]>; 
constructor(public FirebaseData:AngularFireDatabase) { 
    this.Standings = FirebaseData.object('/LeagueStandings'); 
    // Added the following to see the debug 
    this.Standings.subscribe(data => { 
     console.log(data); 
    }); 
} 

HTML采样位:

<ion-col><ion-badge color="secondary">{{Standings.Wins | async }}</ion-badge></ion-col> 

的问题是数据从不显示。然而,使用FirebaseData.list的阵列数据(例如单个游戏统计数据)的相同类型的确行得通。我不知道如何轻松提取要显示的页面的数据结构。

添加控制台转储确实显示从Firebase返回的数据,但我的home.html没有更新。

![Debug console

+0

你在哪里设置'LeagueInfo'? – theblindprophet

+0

对不起,这是一个从编辑代码到小样本复制的错字。我更新了HTML,因为它应该引用排名。 –

+0

我仍在调查它,但你有没有尝试过其他语法?就像那个'排行榜'''赢'''? (远射.. :)) – nisanarz

回答

0

显然读AngularFire2 documentation密切就能解决问题。我注意到文档是如何显示数据的,并且改变了我的代码以匹配,并且它可以工作。 HTML是我必须改变的。

从'@ angular/core'导入{Component}; 从'angularfire2/database'导入{AngularFireDatabase,FirebaseObjectObservable};

排名:FirebaseObjectObservable; 构造函数(public FirebaseData:AngularFireDatabase){ this.Standings = FirebaseData.object('/ LeagueStandings'); }

HTML采样位:

<ion-col><ion-badge color="secondary">{{Standings.Wins | async }}</ion-badge></ion-col> 

变化:

<ion-col><ion-badge color="secondary">{{ (Standings | async)?.Wins }}</ion-badge></ion-col> 

(排名|异步)?赢得是校正。使用异步管道等待对象,然后使用可选字段(?),然后使用字段(.Wins)。