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 ]
}
}
}
}