Disu
Disu

Reputation: 175

Referencing child data in firebase

firebase data structure

I need to fetch data from firebase database(Check the link for data structure snap) 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;
  });

}

Also I do not want to restructure the data structure... I am unable to find reference to the key. I am working with Angular in Ionic

Upvotes: 0

Views: 46

Answers (1)

Ben
Ben

Reputation: 563

To answer your question more thoroughly, you could add the output of console.log(snapshot) to your question for better debugging.

I'm curious if the data is present in the snapshot, you can verify that by using console.log(snapshot). If it is available you could do something like the following to fetch the other items:

snapshot.forEach((billSnapshot) => {
  console.log(billSnapshot.val());
});

This would maybe point you more in the right direction, you could use something like this:

snapshot.forEach((billSnapshot) => {
  console.log(billSnapshot.val());
  switch(billSnapshot.key) {
  case 'dueDate': 
    this.bill.dueDate = billSnapshot.val(); 
    break;
   // Add here other variables you know the name of
  default:
    //Push all the unknown keys to an array you can use later 
    //on your bill object
    this.bill.keys.push(billSnapshot.val())
  }
});

Hope this helps and points you in the right direction,

Upvotes: 1

Related Questions