Monsieur Sam
Monsieur Sam

Reputation: 333

Update all child in Firebase

In a database from Firebase, I need to update the value open and pass it to false for all child. How I can do it in Javascript ? Something like this

let dbCon = firebase.database().ref("/messages/" + *);
    dbCon.update({
      open: false
    });

enter image description here

Upvotes: 3

Views: 3711

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600116

The Firebase Database has no equivalent to SQL's UPDATE messages SET open=false.

To update a node in Firebase, you must first have a reference to that specific node. And to get a reference to a node, you must know the full path to that node.

This means that you'll first need to read the data, then loop over it, and then update each child in turn. In code:

let dbCon = firebase.database().ref("/messages/");
dbCon.once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    child.ref.update({
      open: false
    });
  });
});

Upvotes: 12

Related Questions