【发布时间】:2017-12-02 15:39:15
【问题描述】:
我正在尝试将 firebase 数据库中的所有数据检索到 typescript 中的数组中。以下是相关代码 sn -p :
export class ViewUserPage {
public list = [];
public ref = firebase.database().ref();
public usersRef = this.ref.child('users');
constructor(public navCtrl: NavController, public navParams: NavParams) {}
ionViewDidLoad(){
this.usersRef.orderByChild('tag').equalTo('staff').on('child_added',function(snap){
this.list.push(snap.val());console.log(snap.val());
});
}
}
我得到的错误是“无法读取 null 的属性 'list'”。我认为它发生在
this.usersRef.orderByChild('tag').equalTo('staff').on('child_added',function(snap){
this.list.push(snap.val());console.log(snap.val());
部分。这是完整的错误:
ERROR TypeError: Cannot read property 'list' of null
at main.js:59030
at main.js:30999
at fc (main.js:30861)
at bf (main.js:30926)
at cf (main.js:30925)
at Qg.g.Gb (main.js:31016)
at Ag.g.wd (main.js:30980)
at og.wd (main.js:30970)
at Yf.Xf (main.js:30968)
at ag (main.js:30952)
defaultErrorLogger @ main.js:1436
如何解决这个问题?请帮忙!
【问题讨论】:
-
可以粘贴你的 html 你在哪里使用模板中的列表变量?
-
添加您的完整错误图像,其中可能包括函数名称或与错误相关的任何其他内容
-
@RahulSingh 我还没有在 html 中使用列表数组。这里我只是想将元素存储在数组中。
标签: angular typescript firebase ionic-framework firebase-realtime-database