英文:
Get data based of child val realtime database
问题
以下是你要翻译的内容:
Here is my database layout. I am currently getting data from it by using: myRef.child(userid).child("UserName").get().addOnSuccessListener {
This works great for getting the needed value, in this case the users name "Josh". I can then set the text view on his profile as displaying his name by using this:
myRef.child(userid).child("UserName").get().addOnSuccessListener {
Log.i("firebase", "Got value ${it.value}")
name.setText("Name: ${it.value}")
}.addOnFailureListener{
Log.e("firebase", "Error getting data", it)
}
I need to get the dislikes values that are true so that his profile displays a list of his dislikes. My original attempt was by using the same method as the name but changing the path from "UserName" to "Dislikes", this returned a list of all the values true and false in the dislikes section. I only need/want the true values so I added .equalTo(true)
so it looked like:
myRef.child(userid).child("Dislikes").equalTo(true).get().addOnSuccessListener {
Log.i("firebase", "Got value dis ${it.value}")
dislikes.setText("Dislikes: ${it.value}")
}.addOnFailureListener{
Log.e("firebase", "Error getting data", it)
}
But this crashes with the following.
java.lang.IllegalArgumentException: When using orderByPriority(), values provided to startAt(), startAfter(), endAt(), endBefore(), or equalTo() must be valid priorities.
How can I fix this error? Or is there another way to get and sort only the true values from the database and get them to an array for use?
英文:
Here is my database layout. I am currently getting data from it by using: myRef.child(userid).child("UserName").get().addOnSuccessListener {
This works great for getting the needed value, in this case the users name "Josh". I can then set the text view on his profile as displaying his name by using this:
val name = findViewById<TextView>(R.id.Nameuser)
myRef.child(userid).child("UserName").get().addOnSuccessListener {
Log.i("firebase", "Got value ${it.value}")
name.setText("Name: ${it.value}")
}.addOnFailureListener{
Log.e("firebase", "Error getting data", it)
}
I need to get the dislikes values that are true so that his profile displays a list of his dislikes. My original attempt was by using the same method as the name but changing the path from "UserName" to "Dislikes", this returned a list of all the values true and false in the dislikes section. I only need/want the true values so I added .equalTo(true)
so it looked like:
val dislikes = findViewById<TextView>(R.id.Dislikes)
myRef.child(userid).child("Dislikes").equalTo(true).get().addOnSuccessListener {
Log.i("firebase", "Got value dis ${it.value}")
dislikes.setText("Dislikes: ${it.value}")
}.addOnFailureListener{
Log.e("firebase", "Error getting data", it)
}
But this crashes with the following.
java.lang.IllegalArgumentException: When using orderByPriority(), values provided to startAt(), startAfter(), endAt(), endBefore(), or equalTo() must be valid priorities.
How can I fix this error? Or is there another way to get and sort only the true values from the database and get them to an array for use?
答案1
得分: 0
Firebase实时数据库查询包括两个操作:
- 调用
orderBy...
,告诉数据库按特定值对子节点进行排序。 - 调用
equalTo
、startAt
、endAt
等,告诉数据库随后根据之前排序的值来筛选子节点。
您的代码包含了第二个操作,但没有包含任何对数据排序的指令。尽管从语法上讲是有效的(否则 SDK 会引发错误),但它不会执行您的需求。
在您的情况下,您希望按照 Dislikes
下的子节点的值进行排序/筛选,因此应该是:
myRef.child(userid).child("Dislikes").orderByValue().equalTo(true)...
希望这对您有所帮助。
英文:
Firebase Realtime Database queries consist of two operations:
- A call to
orderBy...
, which tells the database to order the child nodes on a specific value. - A call to
equalTo
,startAt
,endAt
, etc, which tells the database to subsequently filter the child nodes based on the values it previously sorted them on.
Your code contains #2, but it doesn't contain any instruction to order the data. While this is valid syntactically (otherwise the SDK would've raised an error) it doesn't do what you need.
In your case, you want to order/filter on the value of the childes nodes under Dislikes
, so that's be:
myRef.child(userid).child("Dislikes").orderByValue().equalTo(true)...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论