0

Why is it that I can't seem to update a result set. I have 1000 items and I query Firebase and get back a DataSnapShot array. I want to check each element of the array and if a condition exists, then change the "type" to a "C". I get no errors, yet the update does not appear to have been applied. I'm doing this in the promise that is returned and yet the update is "ignored".

var db = admin.database();
var ref = db.ref("/calendars");

ref.once("value", function(snapshot) {

  snapshot.forEach(function(data) {
    var obj = data.val();
    if (someconditionIsTrue) {
      var objToUpdate = `calendars\\${data.key}`;
      console.log(`Should update ${objToUpdate}`);

      var entryRef = db.ref("/calendars").child(data.key).child('type');
      entryRef.transaction(function(t) {
        console.log(`TYPE: ${t}`); // DISPLAYS NULL EVEN THOUGH THERE IS A CHAR IN DB
        return "C"; // Should change TYPE in the record to a "C"
      });
    }

Also tried just to do a set inside the forEach at well but that does not work either (which is why I thought i had to use a transaction).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
justdan0227
  • 1,374
  • 18
  • 48
  • Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will be `null`. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See https://stackoverflow.com/questions/33578887/transcation-updatefunction-parameter-is-null/33578953#33578953 – Frank van Puffelen Nov 21 '18 at 16:21
  • What is the condition you are trying to check? – Frank van Puffelen Nov 21 '18 at 16:25
  • The condition is not really relevant (take the condition out and do an update regardless - it does not update). What it appears that I can not do an update of a record while inside the promise? – justdan0227 Nov 23 '18 at 14:30
  • 1
    I don't see any promise in the code you shared. But [`Reference.transaction` returns a promise](https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction), so it should work nicely with other promises (e.g. in a `Promise.all()` call). – Frank van Puffelen Nov 23 '18 at 15:16
  • Sorry my bad for use of terms. the ref.once "value, function(snapshot)" I consider a "promise"; it’s a code block that is called once the snapshot result is returned. It seams that no mater what I do while inside the "forEach" I can not update data in that array of objects returned if a condition is met and get it to update in the DB – justdan0227 Nov 25 '18 at 15:33

1 Answers1

0

Frank answered this the best way:

Firebase Database transactions are compare-and-set. The callback will immediately be invoked with the best guess that the client has about the current value. Often this will be null. You tell it what the new value would be based on that, and your callback will eventually be invoked again with the updated guess to the current value. See stackoverflow.com/questions/33578887/… – Frank van Puffelen Nov 21 '18 at 16:21

justdan0227
  • 1,374
  • 18
  • 48