【问题标题】:AngularFire2 - Ionic - Cannot read property of undefinedAngularFire2 - 离子 - 无法读取未定义的属性
【发布时间】:2019-02-26 04:11:38
【问题描述】:

HTML 抛出未定义的错误,就好像未检索到数据一样。这是我对 Firebase 列表的服务调用:

服务.ts

 getGames(eventId) {
    var ref =  this.afd.list('/game/', ref => ref.orderByChild('event').equalTo(eventId)).valueChanges();

    return ref;

}

我的页面.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 语句中,我在日志中正确地看到了数据。它会在 html 上引发错误,即使它看起来像是填充了“gamesList”。

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

感谢您的帮助!

【问题讨论】:

  • 您能在问题中添加确切的错误消息吗?

标签: typescript ionic-framework firebase-realtime-database angularfire2


【解决方案1】:

我认为问题是因为在您的 html 上,它试图在从 firebase 获取数据之前循环遍历 gamesList。您需要将其设置为异步。

<ion-item ngFor="let g of gamesList | async">

如果你这样做,你就不需要调用订阅...让异步管道为你处理订阅。

this.gamesList = this.firebase.getGames(this.currentEvent)

【讨论】:

  • 不幸的是,这是我尝试的另一条导致相同错误的路线。我将发布完整的错误。
  • 如果您不使用异步管道,您仍然可以使用与在 subscribe() 中设置 gamesList 相同的方法。但在 html 中,您需要先检查 gameList 是否不为空:*ngIf="gamesList" 然后在其中执行您的 for 循环。
猜你喜欢
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2017-08-02
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多