HTML引发未定义的错误,就像未检索到数据一样。这是我对Firebase列表的服务呼叫:
Service.ts
getGames(eventId) {
var ref = this.afd.list('/game/', ref => ref.orderByChild('event').equalTo(eventId)).valueChanges();
return ref;
}
我的page.ts
currentEvent: any;
gamesList: any[];
constructor(public navCtrl: NavController, public navParams: NavParams, public
firebase: FirebaseProvider) {
this.currentEvent = navParams.get('data');
this.firebase.getGames(this.currentEvent).subscribe((val: any) => {
val.forEach((e) => {
console.log("date " + e.date);
})
this.gamesList = val;
})
}
在foreach语句中,我在日志中正确看到了数据。即使看起来好像已填充“ gamesList”,它也会在html上引发错误。
page.html
<ion-list>
<ion-list-header>Games</ion-list-header>
<ion-item ngFor="let g of gamesList">
{{g.date}}
</ion-item>
</ion-list>
完全错误:
Unhandled Promise rejection: Cannot read property 'date' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'date' of undefined
at Object.eval [as updateRenderer] (LandetailPage.html:20)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14735)
at checkAndUpdateView (core.js:13849)
at callViewAction (core.js:14195)
at execComponentViewsAction (core.js:14127)
at checkAndUpdateView (core.js:13850)
at callWithDebugContext (core.js:15098)
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.js:14635)
at ViewRef_.detectChanges (core.js:11619)
at NavControllerBase._viewAttachToDOM (nav-controller-base.js:460) TypeError: Cannot read property 'date' of undefined
at Object.eval [as updateRenderer] (ng:///AppModule/LandetailPage.ngfactory.js:66:27)
at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:8100/build/vendor.js:15277:21)
at checkAndUpdateView (http://localhost:8100/build/vendor.js:14391:14)
at callViewAction (http://localhost:8100/build/vendor.js:14737:21)
at execComponentViewsAction (http://localhost:8100/build/vendor.js:14669:13)
at checkAndUpdateView (http://localhost:8100/build/vendor.js:14392:5)
at callWithDebugContext (http://localhost:8100/build/vendor.js:15640:42)
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (http://localhost:8100/build/vendor.js:15177:12)
at ViewRef_.detectChanges (http://localhost:8100/build/vendor.js:12161:22)
at NavControllerBase._viewAttachToDOM (http://localhost:8100/build/vendor.js:54343:40)
n.onUnhandledError @ polyfills.js:3
r @ polyfills.js:3
(anonymous) @ polyfills.js:3
n.microtaskDrainDone @ polyfills.js:3
o @ polyfills.js:3
Promise.then (async)
r @ polyfills.js:3
t.scheduleTask @ polyfills.js:3
r.scheduleTask @ polyfills.js:3
r.scheduleMicroTask @ polyfills.js:3
f @ polyfills.js:3
t.then @ polyfills.js:3
NavControllerBase._nextTrns @ nav-controller-base.js:246
NavControllerBase._queueTrns @ nav-controller-base.js:185
NavControllerBase.push @ nav-controller-base.js:70
webpackJsonp.184.LanPage.goToLAN @ lan.ts:58
(anonymous) @ LanPage.html:22
handleEvent @ core.js:13589
callWithDebugContext @ core.js:15098
debugHandleEvent @ core.js:14685
dispatchEvent @ core.js:10004
(anonymous) @ core.js:10629
(anonymous) @ platform-browser.js:2628
globalListener @ platform-browser.js:3196
感谢您的帮助!
答案 0 :(得分:1)
我认为问题是因为在您的html上,它试图在从firebase提取数据之前遍历gamesList。您需要将其设置为异步。
<ion-item ngFor="let g of gamesList | async">
如果您以这种方式进行操作,则无需调用订阅...让异步管道为您处理订阅。
this.gamesList = this.firebase.getGames(this.currentEvent)