根据实时数据库中子值获取数据

huangapple go评论71阅读模式
英文:

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实时数据库查询包括两个操作:

  1. 调用 orderBy...,告诉数据库按特定值对子节点进行排序。
  2. 调用 equalTostartAtendAt 等,告诉数据库随后根据之前排序的值来筛选子节点。

您的代码包含了第二个操作,但没有包含任何对数据排序的指令。尽管从语法上讲是有效的(否则 SDK 会引发错误),但它不会执行您的需求。

在您的情况下,您希望按照 Dislikes 下的子节点的值进行排序/筛选,因此应该是:

myRef.child(userid).child("Dislikes").orderByValue().equalTo(true)...

希望这对您有所帮助。

英文:

Firebase Realtime Database queries consist of two operations:

  1. A call to orderBy..., which tells the database to order the child nodes on a specific value.
  2. 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)...

huangapple
  • 本文由 发表于 2023年3月7日 02:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定