带有childKey的Firebase queryEqualToValue [英] Firebase queryEqualToValue with childKey

查看:223
本文介绍了带有childKey的Firebase queryEqualToValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Firebase数据库中查询数据:

  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("scho‌​olNameLC")
    .queryEqua‌​lToValue("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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆