Node.js: iterating through a JSON tree of a nested firebase

I am trying to access a nested firebase tree from node.js as follows.

ref.forEach( function(snapshot0) {
    snapshot0.child("Meals").forEach( function(snapshot1) {                                                            
        console.log(snapshot1.val);
    });                         
});

      

And he will always tell me that ref.forEach is not a function.

 TypeError: refPrices.forEach is not a function

      

I have defined them as follows.

var db        = admin.database();
var ref       = db.ref("users");

      

I have checked these answers

How do I iterate through a large dataset using Firebase / node.js?

Iterating through nested firebase objects - Javascript

Iterate through nested JSON tree and change values

But I believe in line with my above code should work, so I'm really confused. Also, I tried to replace forEach with a loop, but I get

TypeError: ref.length is not a function

      

so I cannot calculate the maximum value of the loop. I have also tried

ref.once("value", function(snapshot0) {
    snapshot0.child("Meals").forEach( function(snapshot1) {                                                            
        console.log(snapshot1.val);
    });                         
});

      

but in this case it throws an error

TypeError: snapshot0.forEach is not a function

      

so I'm pretty much all the same problems. I can't figure out where my example differs from the solution links above?

Edit: this is what my JSON tree looks like (as requested by Frank). I have shortened the user ID a bit.

"users" : {
    "ugkvjTuXW2" : {
        "Meals" : {
            "Meal_2" : {
                "priceHist" : [ null, 1, 1, 1, 1, 1, 1 ]
            },
            "Meal_3" : {
                "priceHist" : [ null, 1, 1, 1, 1, 10, 1 ]
            },
            "Meal_4" : {
                "priceHist" : [ null, 1, 1, 1, 1, 1, 1 ]
            },
            "Meal_5" : {
                "priceHist" : [ null, 1, 1, 1, 1, 1, 1 ]
            }
         }
    },
    "oJJ1Cojia2" : {
        "Meals" : {
            "Meal_2" : {
                "priceHist" : [ null, 1, 4, 4, 1, 1, 1 ]
            },
            "Meal_3" : {
                "priceHist" : [ null, 1, 1, 1, 1, 10, 1 ]
            },
            "Meal_4" : {
                "priceHist" : [ null, 1, 5, 1, 1, 7, 1 ]
            },
            "Meal_5" : {
                "priceHist" : [ null, 3, 1, 1, 1, 12, 1 ]
            }
         }
    }
} 

      

+3


source to share


1 answer


In your first snippet: DatabaseReference

is nothing but a path to some data in your Firebase database. The data itself is not specified in the ref, so you cannot iterate over the data.

It's hard to tell what's going on in the second snippet, because we don't know what the data underneath looks like /users

. Modify your question to include the minimum JSON snippet (as text, no screenshots) needed to reproduce the issue. You can get this by clicking the Export JSON link in the Firebase Database Console .

Update



If you fetch the value /users

, you get a snapshot with all users. So the first level of children is a node for each user. Then underneath, you eat for each user. Your code is missing a loop for the first level, so:

var ref = admin.database().ref("users");
ref.once("value", function(userSnapshot) {
    userSnapshot.forEach(function(userSnapshot) {
        userSnapshot.child("Meals").forEach(function(mealSnapshot) {                                                                
            console.log(mealSnapshot.val());
        });                         
    });
});

      

Also note that you are using numeric indices, which Firebase recommends against. See this blog post on Arrays and Firebase Documentation on Handling Collections .

+4


source







All Articles