【问题标题】:Firebase realtime database retrive data by uid and put it to Varable kotlin androidFirebase 实时数据库通过 uid 检索数据并将其放入 Variable kotlin android
【发布时间】:2020-05-08 18:32:55
【问题描述】:

如何从实时数据库中检索数据? 我想通过 uid 获取用户特定的数据搜索,女巫也是用户数据库的子项。 我需要检索特定用户 ID 的所有数据 “日”、“月”等。值作为变量。

我在 firebase 中有数据:

DatabaseRow 类:

data class DatabaseRow (
    val id:String = "",
    val names: String= "",
    val lastname: String="",
    val day: String="",
    val month: String="",
    val year:String=""
)

firebase 输入:

 val firebaseInput = DatabaseRow(user,names,lastname,day,month,year)
        userRef.child("$user").setValue(firebaseInput)

要检索数据,我正在使用此代码:

  auth = FirebaseAuth.getInstance()
        FirebaseUser = auth.getCurrentUser()!!

        val uid = auth.getUid()!!

        val fireBase = FirebaseDatabase.getInstance()
        userRef = fireBase.getReference("users")
        val ordersRef = userRef.child("$uid").equalTo("$uid")


val valueEventListener = object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (ds in dataSnapshot.children) {
                    val username = ds.child("day").getValue(String::class.java)
                    Log.d("DataBaseGetName",username)
                    this@Main2Activity.testTextView.text = username 
                }
            }

            override fun onCancelled(databaseError: DatabaseError) {
                Log.d("Data", databaseError.getMessage()) //Don't ignore errors!
            }
        }
   ordersRef.addValueEventListener(valueEventListener)

这是数据库规则:

{
  "rules": {
      ".read": true,
      ".write": true,
         "users":{
            "$uid":{
               ".write": "auth.uid === $uid",
               ".read": "auth.uid === $uid"
               },     
    "users":{
        ".indexOn": [".value","id","names","lastname","day","month","year"]
    },
   }
  }  
}  

【问题讨论】:

  • 请解释一下上面的代码有什么问题以及为什么使用this@Main2Activity.textTextView.text
  • 感谢您进行语法编辑
  • 我没有在日志或任何地方检索到任何数据。我正在使用文本视图来查看是否从数据库中检索到任何数据。

标签: android firebase kotlin firebase-realtime-database


【解决方案1】:

当您收到uid 时,您正在尝试将其与数据库uid 进行比较。 uid 总是不同的。

val ordersRef = userRef.child("$uid")
val valueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        val username = dataSnapshot.child("names").getValue(String.class);
        Log.d("DataBaseGetName",username)
    }

    override fun onCancelled(databaseError: DatabaseError) {
        Log.d("Data", databaseError.getMessage()) //Don't ignore errors!
    }
}
ordersRef.addValueEventListener(valueEventListener)

【讨论】:

  • 每个用户的 Uid 都不同,因为它是通过电子邮件进行的 firebase 身份验证。并且应用程序将数据保存在登录用户 Uid 下。
  • 是的,这就是为什么您在尝试检索单个用户数据时不需要放置 equalto 的原因。当多个用户持有相似类型的数据时使用equalto
  • 当我尝试通过 Uid child 检索数据时 Uid child 没有被索引并且数据库不会读取数据并发出使用未指定索引的警告
  • 你能帮我使用它们吗?
猜你喜欢
  • 2020-09-18
  • 2022-01-17
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多