Reputation: 655
I have below firebase database structure.I need to delete the whole key having specific dMoble
value
I have tried below code but its not working
String emailID= singleToneClass.getInstance().getData();
mDataBase=FirebaseDatabase.getInstance();
mReferenceBooks=mDataBase.getReference("books");
mReferenceBooks.orderByChild("dMoble").equalTo(emailID)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mReferenceBooks.child(dataSnapshot.getKey()).removeValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 0
Views: 42
Reputation: 80944
Change this:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mReferenceBooks.child(dataSnapshot.getKey()).removeValue();
}
Into this:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
mReferenceBooks.child(ds.getKey()).removeValue();
}
Your reference is at node books
, to access the key
you need to iterate inside of it and then use getKey()
Upvotes: 1