gpt4 book ai didi

json - 从 firebase 检索特定数据

转载 作者:行者123 更新时间:2023-11-28 12:43:54 25 4
gpt4 key购买 nike

我正在尝试从我的 Firebase database 中检索一些数据,但我无法完成。首先,我创建了包含数据的 .json 文件,然后将其导入到 Firebase 中。看起来像这样,但要长得多:

enter image description here

现在我正在尝试打印出所有“品牌”,例如“CATCH”、“CRAFTED SNUS”等,但由于某些原因我不能。

我就是这样做的。我在 viewDidLoad() 中加载它。我知道这样做是不对的,只是为了测试:

func fetchBrands(){
ref = FIRDatabase.database().reference()
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

if !snapshot.exists() { return }

//print(snapshot)

if let snusBrand = snapshot.value!["Brands"] as? String {
print(snusBrand)
}

// snapshot.childSnapshotForPath("full_name").value as! String
})


}

我做错了什么?我还将 readwrite 规则从 Firebase 设置为 true

它唯一打印的是:

enter image description here

最佳答案

您可以尝试更新 ref 以直接指向子品牌并像这样迭代返回的快照:

ref = FIRDatabase.database().reference().child("Brands")

ref.observeEventType(.Value, withBlock: { snapshot -> Void in
for brands in snapshot.children {
print(brands)
}
})

关于json - 从 firebase 检索特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com