Hammad Nasir
Hammad Nasir

Reputation: 2959

Value not getting removed from Firebase

I'm trying to remove some value stored at a particular reference on Firebase. Here's how I have stored it:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).setValue(MainActivity.uid);

Here's how I'm trying to remove it:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).child(MainActivity.uid).removeValue();

itemIDFU has been generated in onCreate() using this code:

itemIDFU = mDatabase.child("users").child(uid).push().getKey();

The problem is that it is getting stored successfully, but not getting removed. What's going wrong here? Please let me know.

Upvotes: 0

Views: 1417

Answers (2)

Ryan
Ryan

Reputation: 2008

It looks like when you set the value, you set the key itemIDFU to equal whatever value is in MainActivity.uid

However, when you delete the data, you are trying to get the child of itemIDFU, which doesn't exist.

You didn't add MainActivity.uid as the child of itemIDFU, you added it as the value.

You have two possible solutions:

Try this if you want to remove the value, but leave the itemIDFU key there empty:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).setValue(null);

Try this if you want to completely remove the key and value:

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).removeValue();

Here is a picture to help illustrate the data and the problem:

enter image description here

One more picture to explain

enter image description here

Upvotes: 3

Kamal Das
Kamal Das

Reputation: 147

You can remove values in these two ways:

//first way to remove a value

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).setValue(null);

//or try this

mDatabase.child("users").child(uid).child("followersUID").child(itemIDFU).removeValue();

I feel that the value is not getting removed as you are adding .removeValue() on value of child rather than child itself.

Upvotes: 1

Related Questions