Thursday 20 April 2017

Looping and Adding data together in Firebase with Cloud Functions

I am a swift iOS programmer and I'm quite new to firebase function and the language of node.js. I have a firebase database structure that shows the exercise details of different users in one "Gym". Each User's exercise has the distance travelled, the time in minutes and the pace (in seconds and minutes) See the json code representig the Club.

 {
  "Gym" : {
    "-KhnVNdA50oR04q073k5" : {
      "distance" : 0.01,
      "minutes" : 6,
      "pace" : 7.5,
      "seconds" : 40
    },
    "-KhqiOc0CGdFMXihGB2N" : {
      "distance" : 3.54,
      "minutes" : 5,
      "pace" : 5.89,
      "seconds" : 23
    },
    "-KhrfOsis4m51qM9kbl2" : {
      "distance" : 3.90,
      "minutes" : 2,
      "pace" : 6.77,
      "seconds" : 38
    },
    "-Ki7NswN2bQb0n1OIuyr" : {
      "distance" : 4.53,
      "minutes" : 10,
      "pace" : 6.10,
      "seconds" : 29
    }
  }
}

I would like to write a firebase function (in node.js) to loop through the data and add all the seconds together onWrite. From tutorials I know how to log information into the console and so on. But I do not know how to loop through data - something that's extremely easy in Swift.

Here is my starting point

exports.totalClubSeconds = function.database.ref('/ClubRuns/{RunID}').onWrite(event => {

    const runID = event.params.RunID


});

Much appreciation for the help in advance. Apologies for having the function empty. Node.js has a bit of a learning curve and learning time is a little tight. :(



via Gugulethu

No comments:

Post a Comment