Reputation: 167
I'm an iOS developer with very little experience in both Javascript and server code so I'm a little lost here.
I'm getting an error when I create a new user in firebase and trigger a function to create a new user in stripe. Here is my firebase function straight from Stripe's docs.
exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => {
const customer = await stripe.customers.create({email: user.email});
return admin.firestore().collection('stripe_customers').doc(user.uid).set({customer_id: customerId});
});
I successfully create a new user in Stripe with a customer ID.
I get is this error in my firebase logs and don't capture the customer ID so I can save it in firestore.
I'm not sure what I'm doing wrong or how to interpret this message. Any pointers would be greatly appreciated.
createStripeCustomer
ReferenceError: customerId is not defined at exports.createStripeCustomer.functions.auth.user.onCreate (/srv/index.js:120:93) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)
I have also tried this return changing customerId to ID
return admin.firestore().collection('stripe_customers').doc(user.uid).set({customer_id: ID});
Upvotes: 0
Views: 665
Reputation: 167
Here is where I finally ended up. I used a promise instead of await.
exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => {
const stripePromise = new Promise((resolve, reject) => {
stripe.customers.create({
email: user.email
}, (err, customer) => {
if (err) {
reject(err)
} else {
resolve(customer);
stripePromise
.then(customer => {
return admin.firestore()
.collection('stripe_customers')
.doc(user.uid)
.set({customer_id: customer.id});
})
.catch(error => {
console.log(`error resolving promise ${error}`)
})
}
});
})
});
Upvotes: 0
Reputation: 317467
It looks like, from the documentation, that the response object contains an id property. Perhaps you meant to write this line instead:
return admin.firestore()
.collection('stripe_customers')
.doc(user.uid)
.set({customer_id: customer.id}); // use the ID property here
Upvotes: 1