admin管理员组

文章数量:1419229

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 it will just always tell me that ref.forEach is not a function.

 TypeError: refPrices.forEach is not a function

I have defined these as follows.

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

I have checked these answers

How do I iterate through a large dataset with Firebase / Node.js?

Iterating through nested firebase objects - Javascript

Iterate through nested JSON tree and change values

But I believe according to those my above code should be working, so I am really confused. Also, I have tried to replace forEach by 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 that case it throws the error

TypeError: snapshot0.forEach is not a function

so I pretty much still have the same problem. I cannot work out where my example is different from the links to the solutions above?

Edit: here is what my JSON tree looks like (as per Frank's request). I have only shortened the userID's a little 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 ]
            }
         }
    }
} 

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 it will just always tell me that ref.forEach is not a function.

 TypeError: refPrices.forEach is not a function

I have defined these as follows.

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

I have checked these answers

How do I iterate through a large dataset with Firebase / Node.js?

Iterating through nested firebase objects - Javascript

Iterate through nested JSON tree and change values

But I believe according to those my above code should be working, so I am really confused. Also, I have tried to replace forEach by 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 that case it throws the error

TypeError: snapshot0.forEach is not a function

so I pretty much still have the same problem. I cannot work out where my example is different from the links to the solutions above?

Edit: here is what my JSON tree looks like (as per Frank's request). I have only shortened the userID's a little 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 ]
            }
         }
    }
} 
Share Improve this question edited Aug 6, 2017 at 9:12 AJGronevelt asked Aug 6, 2017 at 1:21 AJGroneveltAJGronevelt 5711 gold badge6 silver badges22 bronze badges 2
  • Can you post the ref variable in detail ? – thelonglqd Commented Aug 6, 2017 at 2:59
  • do you know userId before hand? – grgmo Commented Aug 6, 2017 at 11:40
Add a ment  | 

1 Answer 1

Reset to default 7

On your first snippet: a DatabaseReference is nothing more than the path of some data in your Firebase Database. The data itself is not in the ref, so you can't loop over the data.

It's hard to say what's going wrong in your second snippet, because we have no idea what the data under /users looks like. Please edit your question to include a minimal snippet of the JSON (as text, no screenshots please) that is necessary to reproduce the problem. You can get this by clicking the "Export JSON" link in your Firebase Database console.

Update

If you retrieve the value of /users, you get a snapshot with all users. So the first level of children are a node for each user. Then under there you have the meals 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're using numeric indexes, which Firebase remends against. See this blog post on arrays and the Firebase documentation on handling collections.

本文标签: javascriptnodejs iterate through nested firebase JSON treeStack Overflow