首页 > 解决方案 > 从 Cloud Functions for Firebase 向 iOS 应用返回值

问题描述

我正在尝试使用以下 Cloud Function for Firebase 在 Stripe 和我的 iOS 应用程序之间进行通信。然而,虽然console.log(customer)打印出一个有效的客户 JSON 对象,但我的 iOS 应用程序会收到nil结果。我以错误的方式退回它吗?

exports.regCustomer = functions.https.onCall((data,context) => {
    const email = data.email;

    return stripe.customers.create({
        email: email,
    }, function(err, customer) {
        if (err) {
            console.log(err);
            throw new functions.https.HttpsError('stripe-error', err);
        } else {
            console.log("customer successfully created");
            console.log(customer);
            return customer;
        }
    });                                               
});

标签: iosnode.jsfirebasestripe-paymentsgoogle-cloud-functions

解决方案


您应该使用 Stripe Node.js 库的承诺模式而不是回调模式,请参阅https://github.com/stripe/stripe-node/wiki/Promises

然后,按照这些行修改代码应该可以解决问题:

exports.regCustomer = functions.https.onCall((data, context) => {
    const email = data.email;

    return stripe.customers.create({
        email: email
    })
    .then(function(customer) {
        console.log("customer successfully created");
        console.log(customer);
        return {customer: customer};

    }, function(err) {
        throw new functions.https.HttpsError('stripe-error', err);
    });

});

推荐阅读