创建订阅条失败node.js



我有这个代码,直接从stripe网站复制:

app.post('/createSubscription',async (req, res) => {
let r = (Math.random() + 1).toString(36).substring(7);
console.log(r);
const subscription = await stripe.subscriptions.create({
customer: 'cus_' + r,
items: [
{price: 'price_1InXJuIPT89VeZtCMeR3xQWf'},
],
});
})

然而,当我运行这段代码时,它在控制台中给我一个错误。

to show where the warning was created)
(node:38025) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict`

我不确定这到底是什么意思,因为我从来没有见过这个错误的条纹之前?我在订阅功能中做错了什么?

你应该尝试/捕获任何可能抛出错误的异步函数。例如:

let subscription;
try {
subscription = await stripe.subscriptions.create({
customer: 'cus_' + r,
items: [
{ price: 'price_1InXJuIPT89VeZtCMeR3xQWf' },
],
});
} catch (e) {
console.error(e);
// Do something about the fact that you encountered an error, example:
return res.status(500).send('Internal Error');
}
// Do stuff with subscription

这应该至少记录您的错误,并防止它崩溃的进程。一旦你可以看到导致问题的实际错误,那么你就可以参考stripe的文档。

最新更新