Friday, 12 May 2017

How to get variables from other database nodes with Cloud Function .onWrite (Firebase)

I am trying to set some variables to send to pass into a function if a Firebase node is set to true. I am trying to use the .parent and .val() function to set a customer_id, based on the documentation here: https://firebase.google.com/docs/functions/database-events

exports.newCloudFunction = functions.database.ref('/user/{userId}/sources/saveSource').onWrite(event => {
// Retrieve true/false value to verify whether card should be kept on file
const saveSource = event.data.val();

if (saveSource) {
  let snap = event.data;
  let customer_id = snap.ref.parent.child('customer_id').val();
  console.log(customer_id);
  // pass customer_id into function
}

However, when I try to run this function I get the following error:

TypeError: snap.ref.parent.child(...).val is not a function
at exports.linkCardToSquareAccount.functions.database.ref.onWrite.event (/user_code/index.js:79:56)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
at process._tickDomainCallback (internal/process/next_tick.js:129:7)

How can I reference a node outside the scope of the original onWrite location?



via KVNA

No comments:

Post a Comment