【问题标题】:Firebase - Is there a way to iterate through all documents in a collection, and find a certain value for a field within each of themFirebase - 有没有办法遍历集合中的所有文档,并在每个文档中找到某个字段的特定值
【发布时间】:2020-02-18 02:56:50
【问题描述】:

我正在尝试找到一种方法来遍历集合中的每个文档,并在 Node.js 中为我的 Firebase 数据库每次查找某个字段的值

这是一个与我的结构相同的数据库结构示例:

{
  "users": {
    "tommy": {
      "username": "tom",
      "points": 3
    },
    "bryan": {
      "username": "bry123",
      "points": 7
    }
  }
}

在这种情况下,我希望它为每个用户返回积分。这意味着它第一次返回 3,第二次返回 7。我尝试查看 Firebase 文档,但找不到我需要的内容。有人可以帮忙吗?

【问题讨论】:

    标签: javascript node.js firebase iteration


    【解决方案1】:

    当然,这样的事情应该可以解决问题:

    let db = firebase.database().ref();
    let usersRef = db.child("users");
    users.once("value").then(function(snapshot) {
      snapshot.forEach(function(userSnapshot) {
        console.log(userSnapshot.key); // "tommy", "bryan"
        console.log(userSnapshot.val()); // { "username": "tom", "points": 3 }, { "username": "bry123", "points": 7 }
        console.log(userSnapshot.child("points").val()); // 3, 7
      });
    });
    

    我强烈建议您花更多时间阅读 Firebase 文档、其中的一些教程,并搜索有关该主题的先前问题。

    【讨论】:

    • 谢谢!我一定会仔细看看:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 2017-04-15
    • 2021-10-30
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多