Roggie
Roggie

Reputation: 1217

JS Google Cloud Function Error: Reference.update failed: First argument contains undefined in property

This is a Google Cloud Function in JS and I am getting the following error, the function creates the Stripe customer successfully but it fails to write back to the database.

I am guessing it has something to do with snap.uid, am I correct? and how do I get the uid of the User node that has been created to trigger the function?

Error: Reference.update failed: First argument contains undefined in property

exports.createStripeCustomerFromUser = functions.database.ref('/users/{userId}').onCreate((snap, context) => {

  const userSnap = snap.val(); 

  const first = userSnap.firstName;
  const last = userSnap.lastName;
  const email = userSnap.email;
  const description = first + ' ' + last;

  return stripe.customers.create({
    email: email,
    description: description,
    name: description,
  }).then((customer) => {
    var updatedUserData = {};
    updatedUserData[`/stripe_ids/${customer.id}`] = snap.uid;
    updatedUserData[`/stripe_customers/${snap.uid}/customer_id`] = customer.id;
    return admin.database().ref().update(updatedUserData);

  });

});

Upvotes: 0

Views: 147

Answers (1)

ToraCode
ToraCode

Reputation: 432

In your case, use

context.params.userId

with userId came from

ref('/users/{userId}')

Upvotes: 1

Related Questions