update firebase entries on an interval

Viewed 2473

I want to be able to update certain values in a firebase entry on a minute interval. I am not sure how to do this but have tried the following:

var interval = setInterval(function(){
  database.ref().on('value',function(snapshot){
    snapshot.forEach(function(childSnapshot) {
        var item = childSnapshot.val();
        item.key = childSnapshot.key;
        database.ref(item.key).update({ item.trainTimeLeft: moment(item.trainFrequency).subtract(moment(moment(Date.now())).diff(item.trainTimeStamp,"minutes"),"minutes") });

    });
  });

},60000);

I am using moment.js for time differences (I am trying to make an updating train schedule). All of the entries are on the main branch of firebase.

2 Answers
Related