user1938007
user1938007

Reputation: 459

Firebase: How to access a child by passing the key?

questions
 -KkXXZkVBjaNwnnPtRzU
   date: 
     "19052017"
   question: 
    "Q9"
   uid: 
    "WAX6aezrBzXuXW3RtnnSApMazRb2"

This is the database structure at the moment. I'm writing a code to delete the node based on the submitted key, in this case KkXXZkVBjaNwnnPtRzU. I've obtained the key using the getKey() method and now I want to use the removeValue() on an DatabaseReference object to which I'm passing the key as the reference.

How would I go about doing that?

I should mention that the current key value I'm passing is a string. Is something like this possible at all? Basically the same principle as passing the value to a child() method, but instead of accessing a child of the node, I'd go for its key.

Upvotes: 0

Views: 87

Answers (1)

EtherPaul
EtherPaul

Reputation: 423

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("questions");

myRef.child("KkXXZkVBjaNwnnPtRzU").removeValue();

It's simple as that. Of course, instead of hardcoding the values, you should use String which you've obtained previously.

Upvotes: 3

Related Questions