Search code examples
javascriptjsonnode.jsfirebase-realtime-databaseiso

firebase admin with node.js: update in nested JSON tree


I use the below code on my Node.js admin server to retrieve data from my JSON tree, which works fine and prints the content of all snapshot2's to the console.

ref.once("value", function(snapshot0) {
  snapshot0.forEach( function(snapshot1) {      
    snapshot1.child("Food").forEach( function(snapshot2) {                                                                                                                                                  
      console.log(snapshot2.val());
    });    
  });                         
});

However, how can I edit the data held at snapshot2?

If I try to call e.g.

snapshot2.update({250:42})

then it gives me the following error:

TypeError: snapshot2.update is not a function

I am really confused and think this must be a problem many people have?


Solution

  • You cannot update a snapshot. but you can update a reference, and you can get reference from snapshot by doing

     snapshot.ref
    

    , then you can do this to update

    snapshot.ref.update({250:42})