简体   繁体   English

node.js:遍历嵌套的firebase JSON树

[英]node.js: iterate through nested firebase JSON tree

I am trying to access a nested firebase tree from node.js as follows. 我试图从node.js访问嵌套的firebase树,如下所示。

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. 它总是会告诉我ref.forEach不是一个函数。

 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? 如何使用Firebase / Node.js迭代大型数据集?

Iterating through nested firebase objects - Javascript 迭代嵌套的firebase对象 - Javascript

Iterate through nested JSON tree and change values 迭代嵌套的JSON树并更改值

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 此外,我试图通过循环替换forEach,但我得到了

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). 编辑:这是我的JSON树的样子(根据Frank的要求)。 I have only shortened the userID's a little bit. 我只略微缩短了userID。

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

On your first snippet: a DatabaseReference is nothing more than the path of some data in your Firebase Database. 在您的第一个片段中: DatabaseReference只不过是Firebase数据库中某些数据的路径。 The data itself is not in the ref, so you can't loop over the data. 数据本身不在ref中,因此您无法循环数据。

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. 很难说你的第二个片段出了什么问题,因为我们不知道/users下的数据是什么样的。 Please edit your question to include a minimal snippet of the JSON (as text, no screenshots please) that is necessary to reproduce the problem. 请编辑您的问题,以包含重现问题所需的JSON最小片段(如文本,请不要截图)。 You can get this by clicking the "Export JSON" link in your Firebase Database console . 您可以通过单击Firebase数据库控制台中的“导出JSON”链接来获取此信息。

Update 更新

If you retrieve the value of /users , you get a snapshot with all users. 如果检索/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 recommends against. 另请注意,您使用的是Firebase建议的数字索引。 See this blog post on arrays and the Firebase documentation on handling collections . 请参阅有关阵列的此博客文章以及有关处理集合Firebase文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM