-1

[This is my json dataThis is my code ][1]

I am trying to change the value of the key-value pair in the Firebase database. Lets suppose I have a key value like this:"chemistry":"20".I want to extract the value of the key chemistry and want to update it after adding some number like I add 10 to it so my final key value pair will become "chemistry":"30" .But I am unable to access the key can someone help me with this.

My code goes here:

 mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d("TAG", "signInWithEmail:success");
                            //extracting the current user from the databse
                            FirebaseUser user = mAuth.getCurrentUser();
                            //getting the uid of the user
                            String Uid=user.getUid();
                            //getting the databse instance for the particular user
                            FirebaseDatabase database = FirebaseDatabase.getInstance();
                            //setting the path to the Users/Uid to read the key values
                            DatabaseReference myRef = database.getReference("Users/"+Uid);


                            //textView.setText(myRef);
                            String key=myRef.child("chemistry").getKey();
                            textView.setText(key);

                           // myRef.setValue("Hello, World!");
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w("TAG", "signInWithEmail:failure", task.getException());
                            Toast.makeText(TestActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            //updateUI(null);
                        }

                        // ...
                    }
                });

my json is here:

{
  "Users" : {
    "LMn5GeZixNQNpdQhGzdKNbTgIw53" : {
      "chemistry" : "20",
      "email" : "umairnsr87@gmail.com",
      "lastScore" : "0",
      "maths" : "0",
      "name" : "umair",
      "phone" : "123456789",
      "physics" : "0"
    }
  }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
umairnsr87
  • 91
  • 7

2 Answers2

0

In general, you should add some attempt code of what you are trying to achieve, so that others can better guide you to fix your problems. People is not supposed to write code for you.

Anyway, if your collection is called users. then you would retrieve a specific ID in this way:

db.collection("users").doc(THEID).get()
    .then( data => {
       console.log(data)
    })

use .add(jsondata) and .set(jsondata) for ading and updating

here's the documentation: https://firebase.google.com/docs/firestore/?gclid=CjwKCAjwm4rqBRBUEiwAwaWjjK7_CnO37twhvxrw2-WUxep6ykpTEaJiGnmojoBz74TNylodfkL7DxoCE9gQAvD_BwE

gianni
  • 1,199
  • 2
  • 14
  • 28
0

I am trying to change the value of the key-value pair in the firebase database.

In general when you want to update a value of a property, you should use DatabaseReference's updateChildren(Map update):

Update the specific child keys to the specified values.

Regarding the second issue:

Lets suppose I have a key value like this:"chemistry":"20".I want to extract the value of the key chemistry and want to update it after adding some number like I add 10 to it so my final key value pair will become "chemistry":"30"

Above, I showed you how you can simply update the value of a property, however, if you want to increment the value of your chemistry field in a multiuser environment, to have consistent data, I recommend you see my answer from this post, where I have explained how to update a score field using Firebase Transactions.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193