I'm trying to figure out how to append values to a list in a Firebase object using AngularFire2. The use case is the creation of a simple scoreboard where the score is saved every time it changes by pushing the following Score object to a list:
{
points: [15, 11],
sets: [2, 1],
timestamp: new Date()
}
A Scoreboard object in the database then looks as follows:
{
event: 'Volleybal',
home: 'Londen',
away: 'Manchester',
scores: [Score]
}
Where the scores array is an array of Score objects as described above. I need to be able to perform two tasks:
- Query the
Scoreboardlist in the Firebase database to obtain the correct event (assuming the event is unique). - Update the
scoresarray by pushing newScoreobjects every time the score changes.
Is this the right schema design and how can I perform these tasks using AngularFire2?
It looks like the above schema would cover your use cases. I'd recommend that the
scoresproperty of theScoreboardobject is handled in your code (and stored) as an Object vs. an array.Assuming the
eventproperty is unique to all theScoreboardobjects, you can retrieve this from Firebase with something like the below.However, if you have a unique key inside the object, it might be worth considering to use that key for the
Scoreboardobject itself, so the Scoreboards resource would look like the belowDoing this would allow you to retrieve/update this object like below.
It's worth noting that Firebase doesn't actually store Arrays; if you present Firebase with an array it turns it into an object, with the keys being the indices of the array. https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
Edited answer per comment below To display the latest stored value, you could get the value using something like the below.