【问题标题】:querying firebase data among all children with auth在所有具有身份验证的孩子中查询 Firebase 数据
【发布时间】:2016-10-30 04:56:09
【问题描述】:

我按照this 教程制作了一个具有 Firebase 同步和身份验证的应用程序。但是现在,我不知道如何在所有孩子中进行查询,因为这些孩子有一个基于字典的键。

更具体地说,从这个例子中,你如何从所有“用户”中查询所有“当前城市”

{
  "users" : {
    "1e2f048f-a3a0-4190-afad-81d724ed1997" : {
      "currentCity" : "Arrecifes",
      "currentCountry" : "ARG",
      "currentState" : "BSA",
      "day" : {
        "domingo" : true,
        "jueves" : true,
        "martes" : false,
        "miercoles" : true
      },
      "gender" : "Masculino",
      "name" : "Guillermo H Acosta",
      "position" : "Medio",
      "registered" : true,
      "timeSince" : "22:00",
      "timeUntil" : "23:00",
      "whereToPlay" : "Güemes"
    },
    "39c6ccf9-61ec-446e-9af3-3a87810fab71" : {
      "currentCity" : "Alvear",
      "currentCountry" : "ARG",
      "currentState" : "CRR",
      "day" : {
        "jueves" : true,
        "miercoles" : true,
        "viernes" : true
      },
      "gender" : "Masculino",
      "name" : "Guillermo Acosta",
      "registered" : true,
      "timeSince" : "21:00",
      "timeUntil" : "23:00"
    },
    "4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : {
      "currentCity" : "Cordoba",
      "currentCountry" : "ARG",
      "currentState" : "COR",
      "day" : {
        "miercoles" : true,
        "viernes" : true
      },
      "gender" : "Masculino",
      "name" : "Hsjja",
      "timeSince" : "02:00",
      "timeUntil" : "03:00"
    },
    "509364fd-388b-42ff-91ed-0811811a4ff3" : {
      "day" : {
        "jueves" : true,
        "lunes" : true,
        "martes" : false,
        "sabado" : true
      },
      "name" : "Guillermo Acosta",
      "position" : "Delantero",
      "timeSince" : "13:00",
      "timeUntil" : "14:00",
      "whereToPlay" : "Güemes"
    }
  }
}

【问题讨论】:

  • 您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 Firebase 数据库控制台中的导出按钮轻松获取该文本。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。
  • 感谢您的建议@FrankvanPuffelen 现在已经完成了。

标签: angularjs ionic-framework firebase angularfire firebase-realtime-database


【解决方案1】:

这将解析每个节点并获取 currentCity 键。如果 currentCity 是NULLABLE,我建议将它放在一个单独的节点中。

let ref = // path to users here
ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
    // Put all of the users into a dictionary
    let postDict = snapshot.value as! [String : AnyObject]
    for user in postDict {
        // Set each child of the user as a dictionary
        let data = activity.1 as! [String : AnyObject]

        // Get the specific child
        let currentCity = children["currentCity"] as! String

        // Do something with the data
        print(currentCity)
    }
})

【讨论】:

    【解决方案2】:

    使用 Firebase 数据库,您要么获得一个节点,要么不获得该节点。无法获取查询中每个节点的一部分。

    因此,如果您只想加载所有用户的城市,您应该保留一个仅包含该信息的节点:

    {
      "userCurrentCities" : {
        "1e2f048f-a3a0-4190-afad-81d724ed1997" : "Arrecifes",
        "39c6ccf9-61ec-446e-9af3-3a87810fab71" : "Alvear",
        "4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : "Cordoba",
      }
    }
    

    【讨论】:

    • 好的。我会记住这一点。因此,我不必为每个用户创建一个节点,而是为我需要过滤到我的应用程序中的每个属性创建节点?我说的对吗?
    • 一般来说,在使用 NoSQL 数据库时,您需要根据应用想要使用数据的方式对数据进行建模。有关详细介绍,请参阅NoSQL data modeling 上的这篇文章。
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2017-06-19
    • 2017-07-28
    • 2018-12-05
    • 2020-07-31
    • 2016-12-31
    • 2021-04-22
    • 2021-10-05
    相关资源
    最近更新 更多