gpt4 book ai didi

typescript - Firebase 函数中的 onCall 函数导致 Firebase 函数日志中出现未处理的拒绝错误

转载 作者:行者123 更新时间:2023-12-04 11:00:18 25 4
gpt4 key购买 nike

通过遵循 Firebase Docs 和 Stripe API Docs,我创建了一个函数,该函数根据传递的 token 创建费用。此函数是使用 Firebases onCall 函数设置的,以便能够直接从我的应用程序调用它。

可悲的是,当您调用该函数时,Stripe 返回一个错误,尽管该函数似乎没有将其发送给客户端,因为我的客户认为它是成功的。

index.ts 中的 Firebase 函数

export const oneTimeCharge = functions.https.onCall((data, context) => {
stripe.charges.create({
amount: data.amount,
currency: data.currency,
source: data.source,
description: data.description
}).then((charge) => {
return {'charge':charge};
}).catch((error) => {
throw new functions.https.HttpsError('unknown', 'Something went wrong!', error);
})
});

当我调用此函数时,Firebase 函数日志显示以下两个错误。这让我相信我的 函数.https.HttpsError 是不正确的。

错误 1 ​​
Unhandled rejection

错误 2
Error: Something went wrong!
at stripe.charges.create.then.catch (/srv/lib/index.js:37:15)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)

我在 Angular 中的服务函数将它返回为成功。我错过了什么或做错了什么?我觉得我已经用谷歌搜索了几个小时。

最佳答案

使用 Cloud Functions,您需要返回一个在所有异步工作完成时解决的 promise 。对于可调用类型的函数,该 promise 必须使用要发送给客户端的数据进行解析。现在,您的函数没有从顶级函数返回任何内容。您至少应该尝试返回从 stripe.charges.create(...).then(...).catch(...) 返回的 promise 。 :

return stripe.charges.create(...)
...

请仔细阅读 documentation for callables确保您了解您的义务,并确保您完全了解 Promise 的运作方式,否则事情将不会如您所愿。

关于typescript - Firebase 函数中的 onCall 函数导致 Firebase 函数日志中出现未处理的拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com