【发布时间】:2025-12-07 17:40:01
【问题描述】:
当我尝试通过他的 uid 获取用户信息时,这与 firebase 数据库中的相同,会显示此错误。
uid 以正确的方式传递,我已经检查过了。到数据库的路由很好,我也检查过。
这是我的代码,这可能没有任何错误,因为一切正常,只有 Firebase 有一些问题。
class Profil : AppCompatActivity() {
private var fAuth: FirebaseAuth?=null
private var database: DatabaseReference?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profil)
fAuth= FirebaseAuth.getInstance()
// ✋ Here I checked with uid and uid.toString()
var userCheck = fAuth!!.currentUser?.uid.toString()
getData(userCheck)
// ✋ Here I checked that good uid is get
//tv_firsName.text = userCheck
}
private fun getData(userCheck: String) {
database = FirebaseDatabase.getInstance().getReference("Users")
database!!.child(userCheck).get().addOnSuccessListener{
if(it.exists()){
val firstName = it.child("firstName").value
val lastName = it.child("lastName").value
val position = it.child("position").value
val email = it.child("email").value
tv_Imie.text = firstName.toString()
tv_Nazwisko.text = lastName.toString()
tv_Stanowisko.text = position.toString()
tv_Email.text = email.toString()
}else{
Toast.makeText(this, "User does not exist", Toast.LENGTH_SHORT).show()
}
}.addOnFailureListener{
Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show()
Log.e("firebase", "Error getting data", it)
}
}
}
这是我的数据库结构
我检查了一些方法来解决这个问题。
我看到有人用addValueEventListener 提出了一个解决方案,但随后获取了列表。我希望将单个用户信息包含在我的 textViews 中。我读到这个问题可能与 Firebase 中的错误有关,而不是与代码中的错误有关。
【问题讨论】:
-
请编辑您的问题并将您的数据库结构添加为 JSON 文件。您可以通过单击 Firebase Console 的溢出菜单 (⠇) 中的导出 JSON 来简单地获取它。
-
除此之外,您确定您有互联网连接吗?
-
如果
get()不起作用,我建议使用较旧的addListenerForSingleValueEvent方法。两者(应该)返回完全相同的数据,所以不会有任何区别。
标签: android firebase kotlin firebase-realtime-database