Cloud Function triggered, but not executed

I have following function:

exports.onDataAdded = functions.database.ref('/Lager/Shafts/Rescue/582/582001').onWrite((change, context) => {
    if (change.before.exists()) {
            return null;
          }
          // Exit when the data is deleted.
          if (!change.after.exists()) {
            return null;
          }
          const original = change.after.val();

return change.after.ref('/Lager/Shafts/Rescue/583/583001').set(original);
});

I am trying to keep the count of product 1 equal to the count of product two (Can't put it in the same ID for several reasons). It executes the function and says the status is ok but does not update the new value of product 2. What am I missing?

Upvotes: 0

Views: 65

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598668

This seems like a noop:

exports.onDataAdded = functions.database.ref('/Lager/Shafts/Rescue/582/582001').onWrite((change, context) => {
  if (change.before.exists()) {
    return null;
  }

Or more precisely: it will only get past this code when you delete /Lager/Shafts/Rescue/582/582001, which is not what you seem to be trying. My guess is that you meant the inverse in your check:

  if (!change.before.exists()) {
    return null;
  }

Upvotes: 1

Diego P
Diego P

Reputation: 1758

Please try this, Your function is exiting without executing the update.

exports.onDataAdded = functions.database.ref('/Lager/Shafts/Rescue/582/582001').onWrite((change, context) => {
    if (change.after.exists()) {
        const original = change.after.val();
        return admin.database().ref('/Lager/Shafts/Rescue/583/583001').set(original);
    }
    // Exit when the data is deleted.
    if (!change.after.exists()) {
        return null;
    }
});

Upvotes: 1

Related Questions