创建订阅条带失败 node.js

create subscription stripe failing 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`

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

您应该 try/catch 任何可能引发错误的异步函数。例如:

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