4

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:

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.

1 Answer 1

4

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();
});
Sign up to request clarification or add additional context in comments.

9 Comments

So how do I wait until the data is completely fetched?
The data will be completely fetched at the end of your .subscribe() block. However, without knowing what you want to do with the length of the array or the data itself, it's hard to tell you the best approach or location.
I just want to store all the 'arriving' data into an array.
But I think you already achieved that? Your console output clearly shows that your array contains the fetched data.
No, it's not. In your method getTrackerData() you dispatch an asynchronous call. That is the block you pass into .subscribe(). Your method is finished at this moment. However, only after the data from your request arrives the block will be executed and your array will be updated. That means, usually the block from .subscribe() will be executed AFTER your method has finished.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.