This question has been asked before but I didn't quite understand the answer.
I need to calculate the average of an array of data from Firebase, so I have two steps.
- Retrieve all the data
- Determine how many entries there are in that same data
- Calculate the average
My code:
myDataRef.orderByChild("time").startAt(time-180000).endAt(time).on("child_added", function(snapshot) {
votes = snapshot.val().vote;
//console.log("Vote value:" + votes)
});
myDataRef.orderByChild("time").startAt(time-180000).endAt(time).on("value", function(snapshot) {
numberOfVotes = snapshot.numChildren();
//console.log("Number of Votes:" + numberOfVotes)
});
function calculateAverage(numberOfVotes, votes) {
return eval(votes.join('+')) / numberOfVotes;
}
console.log(calculateAverage)
I think I'm misinterpreting something super basic because I can't figure out how to get the data "out" of the Firebase query and into a function. What am I missing?