简体   繁体   中英

How to Update Firebase Realtime Database a Specific Child Node/

This is my Firebase Realtime Database structure:

I want to update userProfilePic in this structure.

I try my best but this is not being updated

here is my code

database.getReference()
    .child("User")
    .child(FirebaseAuth.getInstance().uid.toString())
    .child("userProfilePic")
    .setValue(uri)
    .addOnSuccessListener { 

    }
    .addOnFailureListener {
        Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show()
    }

Please tell me how I can update in Kotlin.

How to Update Firebase Realtime Database a Specific Child Node

If you want to perform an update , you should not be using setValue() but updateChildren(Map<String, Object> update) , which:

Update the specific child keys to the specified values.

Otherwise, you'll always overwrite the existing data. So to update the URL, please use the following lines of code:

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())

You should refer to currentUser when calling for uid like this:

FirebaseAuth.getInstance().currentUser.uid

Instead of:

FirebaseAuth.getInstance().uid.toString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM