arnoldssss
arnoldssss

Reputation: 488

Need to iterate through firebase data

Im trying to retrieve some data in firebase but I cannot figure out how to retrieve each expire_date value from the receipts tree

enter image description here

There could be 0-N quantity of "LFutx9mQbTTyRo4A9Re5I4cKN4q2" kind of receipts and N quantity of numbers inside last_receipt_info

enter image description here

My code since this is:

ref.child("recipts").once("value").then(function(questionsSnapshot) {
  return questionsSnapshot.forEach(function(questionSnapshot) {
    return console.log(questionSnapshot.val());
  });
});

And this actually brings me the data for all recipts but I don't know how to loop exactly the expire_date value.

I tried this lastely but did not bring data:

  ref.child("recipts").once("value").then(function(a) {
      a.forEach(function(a){
      a.child("latest_receipt_info").once("value").then(function(b) {
          return console.log(b.val());  //Here for every subscription I wanted to make a function
      });
    });
  });

Ideas? Thanks!!

Upvotes: 3

Views: 10670

Answers (1)

Matt Altepeter
Matt Altepeter

Reputation: 956

You can use a for ... in loop:

for (var key in questionSnapshot.val()) {
    // => do what you need
}

I've never worked with firebase, but I think that will do what you need.

Edit:

If I'm understanding this properly, key in each iteration above will be something like LFutx9mQbTTyRo4A9Re5I4cKN4q2? If so, you can just keep nesting for...in loops:

for (var key in questionSnapshot.val()) {
    // key = LFutx9mQbTTyRo4A9Re5I4cKN4q2? something like that?

    for (var key2 in questionSnapshot.val()[key]) {
        // key2 = K_23AEu3cjcIKT1tTHf ???
        for (var item of questionSnapshot.val()[key][key2]['latest_receipt_info']) {
            console.log(item.expires_date);
        }
     }
 }

Edit 2:

for (var key in questionSnapshot.val()) {
    for (var item of questionSnapshot.val()[key]['latest_receipt_info']) {
        console.log(item.expires_date);
    }
}

Upvotes: 2

Related Questions