Search code examples
javascriptfirebasefirebase-realtime-databaseangularfire2

Angularfire2 update all children nodes


In Firebase, I have this db

miodb

I want to update all the children nodes (for example the children nodes of 67-68), setting the key 'status' to 'read'.

So this is how I get items list

this.listaquery = db.list('https://xxxxxxxx.firebaseio.com/chat/'+this.navParams .get('chatRoom'),{
          query: {
              orderByChild: 'id',
              equalTo: this.navParams .get('id2')
          }
        });
      }

And I know this is the code to update a child

this.listaquery.update('-KnFI98Lnm-BYXxR_zoY',{status:'read'});

But if I want to update all the children with one command?


Solution

  • I found this solution (I don't know if it's the best one)

    this.listaquery = db.list('https://delega-facile.firebaseio.com/chat/'+this.navParams .get('chatRoom'),{
      query: {
          orderByChild: 'id',
          equalTo: this.navParams .get('id1')
      },
      preserveSnapshot: true
    });
    
    
    this.listaquery
              .subscribe(snapshots => {
                  snapshots.forEach(snapshot => {
                      this.listaquery.update(snapshot.key,{status :'read'});
                      /*
                      console.log(snapshot.key)
                      console.log(snapshot.val())
                      */
                  });
              })