Padma Kotish
Padma Kotish

Reputation: 41

How to remove another user data in Firebase

I'm making an app which saves the data in two different users (A and B) database. I just need a code for user A to delete the data of user B database. I'm using Firebase as database.

I have a code, but this is not working:

        DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("Volunteer");
    driverRef.orderByKey().equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {

                String key = postsnapshot.getKey();
                DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("Volunteer").child(key).child("requests").child("customerRideId");
                driverRef.removeValue();


            }
        }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });

Upvotes: 0

Views: 62

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

You need to retrieve the userid of the other person from the database, so user A will be able to delete data of user B.

If you have this:

User
 useruidA
     name: userA
     email: [email protected]
 useruidB
     name: userB
     email: [email protected]

when you are retrieving the name of userB, then also retrieve the his id, to be able to delete it.

Alternatively you can do this:

 DatabaseReference data = FirebaseDatabase.getInstance().getReference().child("Users");
data.orderByChild("name").equalTo(userB).addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot data: dataSnapshot.getChildren()){
        data.getRef().removeValue();

              }

        }

that way, you do not need his id only the name.

Upvotes: 2

Related Questions