如何将数据从firebaselistobservable存储到数组? [英] How to store data from firebaselistobservable to an array?

查看:152
本文介绍了如何将数据从firebaselistobservable存储到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据从firebase复制到使用角度2的数组。但我无法将数据推入数组。



以下是代码:

变量

  uid:string =''; 
agencyItems:FirebaseListObservable< any []>;
trackerItems:FirebaseListObservable< any []>;
agencyID:any [] = [];

getData()

  this.af.auth.subscribe(auth => {
if(auth){
this.uid = auth.auth.uid;
}
});

this.getAgencyData();

console.log(AgentID:,this.agencyID);
console.log(Array Length =,this.agencyID.length); //问题在这里:array agencyID仍然是0.

this.getTrackerData();

getAgencyData():

  console.log(获取代理数据); 
this.agencyItems = this.af.database.list('/ agencies /',{preserveSnapshot:true});
this.agencyItems.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot.val()._ id);
this .agencyID.push(snapshot.val()._ id);
});
});



getTrackerData():

$ $ $ $ b $ $ console.log(获取跟踪器数据)$ b pre $ $ $ $ $ $ $ );
this.trackerItems = this.af.database.list('/ tracker /'+ this.agencyID [i]);
this.trackerItems.subscribe(trackerItems => trackerItems.forEach(Titem =>
console.log(Tracker name:+ Titem.name),
));




$ b $ 以下是调试控制台的屏幕截图:





因为我是网络编程的新手,有些代码可能看起来完全没有必要。



我在这段代码中做错了什么?我怎样才能实现同样的功能。

解决方案

问题是在哪里,或者更好的时候,阵列。您在获取数据时进行异步调用,但在数据返回之前您正在检查数组的长度。因此数组仍然是空的。
$ b $

getAgencyData()中尝试以下内容:

  console.log(获取代理数据); 
this.agencyItems = this.af.database.list('/ agencies /',{preserveSnapshot:true});
this.agencyItems.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot.val()._ id);
this .agencyID.push(snapshot.val()._ id);
console.log(Array Length =,this.agencyID.length); //查看数组的长度;
});
//编辑
this.getTrackerData();
});


I'm trying to copy the data from firebase to an array using angular 2. But i'm unable to push the data into the array.

Here's the code:

Variables:

uid: string = '';
agencyItems: FirebaseListObservable<any[]>;
trackerItems: FirebaseListObservable<any[]>;
agencyID: any[] = [];

getData()

this.af.auth.subscribe(auth => {
        if (auth) {
            this.uid = auth.auth.uid;
        }
    });

    this.getAgencyData();

    console.log("AgentID: ",this.agencyID);   
    console.log("Array Length = ",this.agencyID.length);  //PROBLEM HERE: Array agencyID is still 0.

    this.getTrackerData();

getAgencyData():

console.log("Fetching agency data");
    this.agencyItems = this.af.database.list('/agencies/',{preserveSnapshot:true});
    this.agencyItems.subscribe(snapshots => {
        snapshots.forEach(snapshot => {
            console.log(snapshot.val()._id);
            this.agencyID.push(snapshot.val()._id);
        });
    });

getTrackerData():

for (let i = 0; i < this.agencyID.length; i++)     
    {
        console.log("Fetching Tracker data");
        this.trackerItems = this.af.database.list('/tracker/' + this.agencyID[i]);
        this.trackerItems.subscribe(trackerItems => trackerItems.forEach(Titem =>
            console.log("Tracker name: " + Titem.name),
        ));
    }    

Here is the debug console screenshot:

Since i'm a newbie to web programming some code may seem completely unnecessary.

What am I doing wrong in this code? How can I implement the same.

解决方案

The problem is the location where, or better WHEN, you are checking the length of the array. You make an asynchronous call when you fetch the data, but you are checking the length of the array before the data has been returned. Therefore the array is still empty.

Try the following in getAgencyData():

console.log("Fetching agency data");
this.agencyItems = this.af.database.list('/agencies/',{preserveSnapshot:true});
this.agencyItems.subscribe(snapshots => {
    snapshots.forEach(snapshot => {
        console.log(snapshot.val()._id);
        this.agencyID.push(snapshot.val()._id);
        console.log("Array Length = ",this.agencyID.length); // See the length of the array growing ;)
    });
    // EDIT
    this.getTrackerData();
});

这篇关于如何将数据从firebaselistobservable存储到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆