代码之家  ›  专栏  ›  技术社区  ›  Alk

将Firebase的云函数值返回到iOS应用程序

  •  0
  • Alk  · 技术社区  · 6 年前

    我正在尝试使用Firebase的以下云功能在Stripe和我的iOS应用程序之间进行通信。然而,尽管 console.log(customer) 打印出一个有效的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;
            }
        });                                               
    });
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Renaud Tarnec    6 年前

    您应该使用条带节点的承诺模式。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);
        });
    
    });