Updating a Nested Object With AngularFire2

1,421 views
Skip to first unread message

Garrett Sanderson

unread,
Nov 10, 2016, 4:30:54 PM11/10/16
to Firebase Google Group

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"
      }


Thanks in advance!





Kato Richardson

unread,
Nov 11, 2016, 11:42:51 AM11/11/16
to Firebase Google Group
Garrett,

AngularFire2 has a guide on reading and writing data here. You need to call update() if you want the changes to persist to the database.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/d575f47c-68c3-4c64-8a08-efb474de375d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Garrett Sanderson

unread,
Nov 11, 2016, 8:44:53 PM11/11/16
to Firebase Google Group

Kato Richardson

unread,
Nov 14, 2016, 4:21:57 PM11/14/16
to Firebase Google Group
I don't see anything calling this.items.update() in your code here. The data won't be persisted if you don't call the update() method.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Garrett Sanderson

unread,
Nov 18, 2016, 6:44:43 PM11/18/16
to Firebase Google Group
Yes I know that I am not calling update. I guess what I am asking is how do I get to that specific key and value in my database that needs to be passed in the update method. All the songs have a completed key and a value that is default to false. How will it know that I only want one of those songs complete key values to change.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/0135db79-bd69-4ff5-974b-304b85451ae5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Garrett Sanderson

unread,
Nov 18, 2016, 6:44:52 PM11/18/16
to Firebase Google Group
Nevermind Kato. I understand now... I am not grabbing my data in the correct manner. I need to grab it with an observable list so that when I call for a change on it Firebase knows exactly what to change when I call update() with a key and a value.

Thanks!


On Monday, November 14, 2016 at 1:21:57 PM UTC-8, Kato Richardson wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/0135db79-bd69-4ff5-974b-304b85451ae5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages