Trouble in fetching firebase data


#1

I need to fetch data from firebase database. I am able to get data of “dueDate” through {{currentBill?.dueDate}} but don’t know how to get data of other items above it. Following is the code used to fetch data:

ionViewDidEnter(){
      this.firebaseData.getBillDetail(this.navParams.get('billId'))
      .on('value', snapshot => {
        this.currentBill = snapshot.val();
        this.currentBill.id = snapshot.key;
      });
     
    }

Kindle help


#2

Well Im guessing the root id of the bills would be some sort of userId. To get the other bills you would get the data by the userid (KmthCJ10rBqHVIThiPN) and then parse the data on your side to get the dueDate for each bill.


#3

Not sure what you are asking… I am getting dueDate. My worry is the data above it. I want to get data of child of currentBill


#5

What I mean is why dont you pull your data with this id? Instead of pulling with the billId.


#6

Misunderstood… Let me try that … But not sure how will it apply to all similar data?


#7

Sorry I misundestood your data structure. Why dont you do a structure like this:

billId:
  - extraItems:
        -  KnHNPuRsIU ...
        -  KnHNn7RprQ ...
        -  etc ...
  - dueDate: " ... "

So you get bill details, and get the extra items by currentBill.extraItems.


#8

Can’t do that. Data structure is good for me


#9

I see, well the probelm is, the data you get when you fetch the bill details will be a json object. So the attribute names will be those unique child ids, which is a bit messy.

So lets say in this case it would be

this.currentBill['-KnHNPuRsIUSGbxN0jcz'];

So you would have to loop trough the JSON object and get all the data that has those unique IDs.


#10

I tried all ways… Not working . Not sure why its so difficult to refer the child :cry:


#11
ionViewDidEnter(){
      this.firebaseData.getBillDetail(this.navParams.get('billId'))
      .on('value', snapshot => {
        this.currentBill = snapshot.val();
        this.currentBill.id = snapshot.key;
      });
     
    }

How can I refere each child… Can anyone suggest forEach function to capture the child IDs created… Further those ids can be looped to get items contained in these IDs


#12

Well this is a standard way to loop trough json object attributes:

for (let key in this.currentBill) {
    console.log(key + " -> " + this.currentBill[key]);
}

#13

Thanks. It does work. Also back ticks in providers (or others) to route with ${billId} works. Thanks