【问题标题】:Returning Values from Cloud Functions for Firebase to iOS app从 Cloud Functions for Firebase 向 iOS 应用返回值
【发布时间】:2019-03-02 11:40:02
【问题描述】:

我正在尝试使用以下 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;
        }
    });                                               
});

【问题讨论】:

    标签: ios node.js firebase stripe-payments google-cloud-functions


    【解决方案1】:

    您应该使用 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);
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 2017-12-01
      • 1970-01-01
      • 2019-02-04
      • 2017-09-07
      • 2018-01-03
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多