【问题标题】:create subscription stripe failing node.js创建订阅条带失败的node.js
【发布时间】:2021-12-03 17:08:40
【问题描述】:

我有这段代码,直接从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`

我不确定这到底是什么意思,因为我以前从未见过条带的 thsi 错误?我在订阅功能中做错了什么?

【问题讨论】:

标签: javascript node.js stripe-payments


【解决方案1】:

您应该尝试/捕获任何可能引发错误的异步函数。例如:

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 的文档。

【讨论】:

    猜你喜欢
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2021-12-12
    • 2019-06-25
    • 2022-08-24
    • 1970-01-01
    相关资源
    最近更新 更多