带有childKey的Firebase queryEqualToValue [英] Firebase queryEqualToValue with childKey
问题描述
queryEqual(toValue:Any?,childKey:String? )
我的数据库结构:
<$ p $学校{
testUID1 {
schoolNameLC:test school 1
}
}
我的查询是:
$ p $ databaseReference.child(Schools) .queryEqual(toValue:test school 1,childKey:schoolNameLC)。observe(.value){(snap)in
print(snap)
}
这个查询输出 null
,我不能完全使它工作。由于我的应用程序设置的方式,我不知道关键 schoolNameLC
有一个值 testSchool1
下 testUID1
的父键。我想要做的就是搜索我的数据库中的 Schools
,并返回任何一个 schoolNameLC
值 test test 1
。
queryEqualToValue :childKey:
(和它的兄弟 queryStartingAtValue:childKey:
和 queryEndingAtValue:childKey:
)不幸的是Firebase数据库API中一些最被误解的方法。
为了让孩子订购,然后过滤该孩子的值,必须调用 queryOrderedByChild()。queryEqualToValue()
。所以如@ ElCaptainV2.0所说:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ databasereference.child(Schools)
.queryOrderedByChild(scho (test school 1)
如果也要从具有相同匹配测试的所有节点中的特定键开始,请使用
价值。当你试图分页/无限滚动结果的时候,这个重载是非常有用的。 childKey:
参数学校1
I am trying to query data in my Firebase database using:
queryEqual(toValue: Any?, childKey: String?)
My database structure:
Schools {
testUID1 {
schoolNameLC: test school 1
}
}
My query is:
databaseReference.child("Schools").queryEqual(toValue: "test school 1", childKey: "schoolNameLC").observe(.value) { (snap) in
print(snap)
}
This query prints out null
and I can't quite get it to work. Because of the way my app is set up, I don't know that the key schoolNameLC
has a value testSchool1
under the parent key of testUID1
. All I want to do is search through the Schools
in my database and return anything with a schoolNameLC
value of test school 1
.
The two-parameter queryEqualToValue:childKey:
(and its brethren queryStartingAtValue:childKey:
and queryEndingAtValue:childKey:
) are unfortunately some of the most misunderstood methods in the Firebase Database API.
To order by a child and then filter on a value of that child, you have to call queryOrderedByChild().queryEqualToValue()
. So as @ElCaptainV2.0 said:
databaseReference.child("Schools")
.queryOrderedByChild("schoolNameLC")
.queryEqualToValue("test school 1")
You only should use the childKey:
parameter if you also want to start at a specific key in all nodes that have the same matching test school 1
value. This overload is really mostly useful when you're trying to paginate/endless scroll results.
这篇关于带有childKey的Firebase queryEqualToValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!