Cobe Ranko
Cobe Ranko

Reputation: 11

How to compare value and delete from firebase realtime database?

I am using Firebase realtime database in Android app, and have data like this:

This is my database

How can I access that data from javascript. I just wanna code function to get value of every endTime and compare with current time and if endTime <= Current Time then delete values yellow marked on anther image.

Yellow Marked Values

Events is fixed value.

Indjija, Vojvodina and -KsB2mVkpgQe_fRyFFH4 are auto generated so it's possible to have more values like that two.

I know how to solve that in android studio with Java code but I'm interesting about implementing server functions.

Upvotes: 1

Views: 1512

Answers (1)

Ariane Breton
Ariane Breton

Reputation: 1333

The answer to this question is buried deep within Firebase Database Reference. forEach() is used to get to the child without knowing the child's exact path.

var leadsRef = database.ref('leads');
leadsRef.on('value', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();
    });
});

Now childSnapshot will contain the required data, the same thing can also be accessed using child_added.

leadsRef.on('child_added', function(snapshot) {
      //Do something with the data
});

The only difference is that in case of forEach(), it will loop through from the start so if any new data will be added, it will also load the previous data but in case of child_added, the listener is only on new child and not the previous existing childs.

Useful Link:

Firebase Read and Write Web

Hope it helps.

Upvotes: 2

Related Questions