Updating a Nested Object With AngularFire2
I am trying to update my data when a user clicks that they have completed a song.
I have already got that user and his array of songs from a service after he/she has logged in.
Now when the user clicks complete I want the data to persist. Currently it will only stay completed until the page refreshes. Meaning the database isn't updating fully. Basically I am needing to update a nested object in a JSON file.
Any help and explanation on this would be greatly appreciated. Thanks!
My code is below.
This will update the song until the current page is refreshed.
songCompleted(name:string) {
for (let song of this.activeUserSongs) {
if (song.name == name) {
song.completed = true;
}
}
}
The Firebase Documentation says to do this...
export class AppComponent {
items: FirebaseListObservable<any>;
constructor(af: AngularFire) {
this.items = af.database.list('/messages');
}
addItem(newName: string) {
this.items.push({ text: newName });
}
updateItem(key: string, newText: string) {
this.items.update(key, { text: newText });
}
deleteItem(key: string) {
this.items.remove(key);
}
deleteEverything() {
this.items.remove();
}
}
Current JSON Looks like this
"-KVMWnS8f0ZthDY5mP7G" : {
"firstName" : "Garrett",
"lastName" : "Sanderson",
"partLeader" : false,
"singingPart" : "First Tenor",
"songs" : [ {
"completed" : false,
"difficulty" : "Medium",
"name" : "America The Beautiful",
"parts" : [ {
"name" : "Baritone",
"urls" : {
"pianoWords" : "http://www.taabc.org/check-off-songs/baritone/america-the-beautiful/america-the-beautiful-piano-words.mp3"
}
]}
]}
Hey @thegrid22593! Can you please fill out your issue with the issue template? It's really important to know the versions of Firebase, Angular, Angular CLI, and AngularFire upfront, and we can answer much faster and accurately when following the template's format. I'm closing the issue, but feel free to open a new one with the template.
Thanks :)
You got it!