ios - 从 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;
}
});
});
解决方案
您应该使用 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);
});
});
推荐阅读
- node.js - 构建 TypeScript monorepo 项目的问题
- python - 无法使用firefox驱动打开网站
- azure - hdfs dfs -setfacl 在 Azure Data Lake Store 上失败
- navigation - PugsJs 为 mixin 添加了额外的类
- javascript - 从字符串中替换所有出现的 \n\r
- oracle - 如何将 R 表导出到 Oracle 数据库
- java - 对于 api 低于 26 的设备,base 64 编码是否有替代方案?
- vb.net - 我如何将数据绑定到新的绑定
- excel - 如何使用 Excel 中的 DGET 函数从数据透视表中提取数据?
- r - 如何在 R 中的 tableGrob() 下对齐 ggparagraph() 文本