如何在 Firebase 数据库中进行查询以获取控制台中的一些子项?例如,从下面的快照中,我如何进行查询以获取Image
s where Des: 11
.
我正在使用这段代码:
func loadData(){
Ref=FIRDatabase.database().reference().child("Posts")
Handle = Ref?.queryOrdered(byChild: "11").observe(.childAdded ,with: { (snapshot) in
if let post = snapshot.value as? [String : AnyObject] {
let img = Posts()
img.setValuesForKeys(post)
self.myarray.append(img)
self.tableView.reloadData()
}
})
}
正如 El Capitan 所说,你需要使用这个:
func loadData(){
let ref = FIRDatabase.database().reference().child("Posts")
ref?.queryOrdered(byChild: "Des").queryEqual(toValue: "11").observe(.childAdded, with: { snapshot in
if let post = snapshot.value as? [String : AnyObject] {
// do stuff with 'post' here.
}
})
}
另外,正如他提到的,您需要设置安全规则,以允许您按“Des”或您要查询的任何其他子节点进行搜索。此外,您需要设置规则以允许您对查询位置进行读取访问:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"Posts": {
"$someID": {
".indexOn": ["Des"]
}
}
}
}
但是,您不会获得“仅图像”,您的查询将返回整个节点。在这种情况下,上面的查询将返回 rrrrrr 节点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)