functions-samples icon indicating copy to clipboard operation
functions-samples copied to clipboard

TypeError: Cannot read property 'customer_id' of undefined

Open willvlad opened this issue 6 years ago • 3 comments

Deployed as per the example. But keep on getting these errors from calling the functions from index.html:

createStripeCharge

TypeError: Cannot read property 'customer_id' of undefined
    at exports.createStripeCharge.functions.firestore.document.onCreate (/srv/index.js:35:34)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

addPaymentSource

TypeError: Cannot read property 'customer_id' of undefined
    at exports.addPaymentSource.functions.firestore.document.onCreate (/srv/index.js:73:38)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

To exclude any rules issues, I've allowed read and write firestore.rules to anyone with uid.

Any idea what could be causing these errors?

willvlad avatar Nov 03 '18 22:11 willvlad

On top of the file your requirements then below you see the functions in my application already up and running:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const stripe = require('stripe')(functions.config().stripe.token);
const currency = functions.config().stripe.currency || 'EUR';
admin.initializeApp();
const firestore = admin.firestore();

createStripeCharge

exports.createStripeCharge = functions.firestore.document('/users/{userId}/charges/{id}')
    .onCreate((snap, context) => {
    const val = snap.data();
    return admin.firestore().doc(`/users/${context.params.userId}`)
        .get().then((snapshot) => {
        return snapshot.data();
    }).then((customer1) => {
        const amount = val.amount;
        const customer = customer1.stripe_customer_id;
        const idempotencyKey = context.params.id;
        const source = val.source;
        const charge = { amount, currency, customer, source };
        return stripe.charges.create(charge, { idempotency_key: idempotencyKey });
    }).then((response) => {
        const data = {
            result: response,
            "isDone": true
        };
        return snap.ref.set(data);
    }).catch((error) => {
        return snap.ref.update({ 'error': userFacingMessage(error), "isDone": false });
    });
});

addPaymentSource

exports.addPaymentSource = functions.firestore
    .document('/users/{userId}/sources/{pushId}').onCreate((change, context) => {
    const source = change.data();
    if (source === null) {
        return null;
    }
    return admin.firestore().doc(`/users/${context.params.userId}`).get().then((snapshot) => {
        return snapshot.data();
    }).then((customer) => {
        return stripe.customers.createSource(customer.stripe_customer_id, { "source": source.token });
    }).then((response) => {
        const data = {
            result: response,
            "isDone": true
        };
        return change.ref.set(data);
    }, (error) => {
        return change.ref.update({ 'error': userFacingMessage(error), "isDone": false });
    });
});

alemens avatar Nov 03 '18 23:11 alemens

Thanks @alemens ! So it looks like the example code is not good anymore. Could it be because of the awaits that seem to be highlighted by jshint on my ide (code)?

Have you changed the other functions within the file as well? If so, would you mind posting them as well, many thanks!!

willvlad avatar Nov 04 '18 00:11 willvlad

Any update on this particular problem? I'm experiencing it at the moment and can't find a way around it. Thanks.

warrenhharding avatar Jun 20 '19 10:06 warrenhharding