最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Update all child in Firebase - Stack Overflow

programmeradmin3浏览0评论

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
    });

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
    });

Share Improve this question edited Apr 8, 2018 at 14:33 Frank van Puffelen 600k85 gold badges890 silver badges860 bronze badges asked Apr 8, 2018 at 11:18 Monsieur SamMonsieur Sam 3331 gold badge7 silver badges20 bronze badges 3
  • Update your state through a for loop or so? Your state should be synced to Firebase? – Satej S Commented Apr 8, 2018 at 11:25
  • I edit it to explain, yes it have to be synced to firebase – Monsieur Sam Commented Apr 8, 2018 at 11:28
  • 1 Check out github./tylermcginnis/re-base – Satej S Commented Apr 8, 2018 at 11:44
Add a ment  | 

1 Answer 1

Reset to default 12

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
    });
  });
});
发布评论

评论列表(0)

  1. 暂无评论