My goal is is to create an array on a nodeJS server that is constantly updating based on data that is changing in the backend on the firebase realtime database.
Once the data is in an array, I need to perform a calculation with MathJS, then push this new value into a separate location in my firebase database. I am able to store everything on my node server in an array initially with child_added, perform my calculation, and push it to firebase. I am also able to update my calculation when new submissions are being added from the client into the database.
var db = firebase.database();
// Event Reference
var specificEvent = db.ref('/posts/area/eventID')
var sampleArray = [];
specificEvent.on("child_added", function(snapshot) {
var newPost = snapshot.val();
//push data to array
sampleArray.push(newPost.userSubmittedNumbers);
//calculation
console.log(math.mean(sampleArray));
My problem is that I have no way of tracking which specific item I want removed from the array when child_removed is called. I thought about using the firebase generated key with each post as a key/value pair, but I believe with MathJS, everything needs to be in just an array. Would dropping MathJS and using an alternative, or simply using just code be the only solution?
Any thoughts or suggestions would be greatly appreciated. Thanks!
How about storing the key/value pairs as you already considered, then – assuming you don't mind another library – using Underscore's pluck()
to get the values into an array that you can do the maths on?