user12340963
user12340963

Reputation:

How to get a value from database firebase

I am new in firebase and I am trying to set the score value to 500 in kotlin code, but I do not know how to change it because it has a push key and a authentication uid key, how do I change the value of the score to 500? If you want you can check the image description below:

enter image description here

Upvotes: 0

Views: 44

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

You need to use updateChildren() to update the field:

val user = FirebaseAuth.getInstance().currentUser
val uid = user.uid
val key = database.child("Users").child(uid).push().key

Before updating you need to have the value of the push key and uid. Therefore when storing the data, you can use val key = database.child("Users").child(uid).push().key to store the value of the push() inside a variable. Then later you can update by doing the following:

val database = FirebaseDatabase.getInstance()
val myRef = database.getReference("Users").child(uid).child(key)

val childUpdates = HashMap<String, Any>()
    childUpdates["score"] = 500

    myRef.updateChildren(childUpdates)

You can read here for more info:

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

Upvotes: 0

Related Questions