0

Here is the Firebase view. Password is updating under users, I want to update it under sidd5(userHelper class)->Password with the help of user-entered phone number.

Firebase Database img

Here is the code.

public void updatePassword(View view) {
            //get data from fields
            String userNewPassword = newPassword.getEditText().getText().toString().trim();
            String phoneNumber = getIntent().getStringExtra("phone");


            //update data in firebase
            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");
            reference.child(phoneNumber).getRef().getParent().child("password").setValue(userNewPassword);

            startActivity(new Intent(getApplicationContext(), ForgetPassSuccess.class));
            finish();
        }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

To find a user with a specific phone number in the Firebase Realtime Database, you need to use a Query object. Once you have found it, then you can simply update the "password" property using "setValue()" method. Assuming that "phone" property holds the exact value that exists under users/sidd5/password, which is "+9.......01", please try the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
Query queryByPhone = usersRef.orderByChild("phone").equalTo(phoneNumber);
queryByPhone.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot userSnapshot : task.getResult().getChildren()) {
                userSnapshot.child("password").getRef().setValue(userNewPassword);
            }
        } else {
            Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I am getting error at '.get()' as Cannot resolve method get in Query. @AlexMamo – Siddhesh Salunke May 05 '21 at 10:52
  • In that case, please update Firebase Realtime Database dependency to the [latest available version](https://firebase.google.com/support/release-notes/android) and tell me if it works. – Alex Mamo May 05 '21 at 11:17
  • Yes it did work. But my password is not updating in Database. It is showing as follows **I/RepoOperation: get for query /users falling back to disk cache after error: Index not defined, add ".indexOn": "phone", for path "/users", to the rules D/NewPassword: Index not defined, add ".indexOn": "phone", for path "/users", to the rules**. @AlexMamo – Siddhesh Salunke May 05 '21 at 12:24
  • Have you tried add the [indexOn](https://stackoverflow.com/questions/34968413/error-index-not-defined-add-indexon)? Does it work that way? – Alex Mamo May 05 '21 at 13:16
  • Yes now it's updating in password section. Thank you sir for giving your precious time. @AlexMamo – Siddhesh Salunke May 05 '21 at 14:38