我不能让这个为我的生活而工作。它永远不会返回结果。帮助将不胜感激。
CODE:
let ref1 = Database.database().reference().child("profiles").child("zig__zag_@hotmailcom").queryOrdered(byChild: "name").queryEqual(toValue : "Simon Lacasse")
ref1.observe(.value, with:{ (snapshot: DataSnapshot) in
for snap in snapshot.children {
print((snap as! DataSnapshot).key)
}
})
Firebase规则:
{
"rules": {
".read": true,
".write": true,
"profiles": {
".indexOn": ["name"]
}
}
}
答案 0 :(得分:0)
你试图做两次同样的事情:
let ref1 = Database.database().reference().child("profiles").child("zig__zag_@hotmailcom").queryOrdered(byChild: "name").queryEqual(toValue : "Simon Lacasse")
您首先要转到/profiles/zig__zag_@hotmailcom
(特定用户的节点),然后查询其下的所有子节点,查找属性name
等于Simon Lacasse
的子节点。如果你检查/profiles/zig__zag_@hotmailcom
,你会发现它的子节点已经是单独的属性,所以你在JSON树中的位置太深了。
要么你需要直接查找:
let ref1 = Database.database().reference().child("profiles").child("zig__zag_@hotmailcom")
ref1.observe(.value, with:{ (snapshot: DataSnapshot) in
print(snapshot.key)
})
在这种情况下不需要循环,因为您已经在查找要加载的精确节点。
或者您查询与名称匹配的子节点:
let ref1 = Database.database().reference().child("profiles").queryOrdered(byChild: "name").queryEqual(toValue : "Simon Lacasse")
ref1.observe(.value, with:{ (snapshot: DataSnapshot) in
for snap in snapshot.children {
print((snap as! DataSnapshot).key)
}
})
这里需要循环,因为可能有多个子节点与查询匹配。