[英]How to update value of a particular child node in firebase realtime database?
[英]How to Update Firebase Realtime Database a Specific Child Node/
這是我的 Firebase 實時數據庫結構:
我想在這個結構中更新userProfilePic
。
我盡力了,但這沒有更新
這是我的代碼
database.getReference()
.child("User")
.child(FirebaseAuth.getInstance().uid.toString())
.child("userProfilePic")
.setValue(uri)
.addOnSuccessListener {
}
.addOnFailureListener {
Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show()
}
請告訴我如何在 Kotlin 中進行更新。
如何更新特定子節點的 Firebase 實時數據庫
如果要執行update ,則不應使用 setValue() 而應使用updateChildren(Map<String, Object> update) ,其中:
將特定子鍵更新為指定值。
否則,您將始終覆蓋現有數據。 因此,要更新 URL,請使用以下代碼行:
val update = mapOf("userProfilePic" to uri) //👈
database.getReference()
.child("User")
.child(FirebaseAuth.getInstance().currentUser.uid) //👈
.updateChildren(update)
.addOnSuccessListener {
Toast.makeText(this,"Update successful.",Toast.LENGTH_SHORT).show()
}
.addOnFailureListener {
Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show()
}
我認為您應該重新考慮此代碼段
child(FirebaseAuth.getInstance().toString())
當像這樣調用uid
時,您應該參考currentUser
:
FirebaseAuth.getInstance().currentUser.uid
代替:
FirebaseAuth.getInstance().uid.toString()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.