未处理的承诺拒绝条纹 paymentIntent 示例

Unhandled promise rejection stripe paymentIntent example

我正在尝试使用 node 和 express 设置条纹支付应用程序,按照此处的示例: https://stripe.com/docs/payments/accept-a-payment#web

我按照指示在我的服务器端应用程序代码中创建了路由,并将客户端代码插入到我的 html 文件中。我正在尝试创建没有模板引擎的应用程序,只是 html/css/javascript/node.

var response = fetch('/secret').then(function(response) {
  return response.json();
}).then(function(responseJson) {
  var clientSecret = responseJson.client_secret;
  // Call stripe.confirmCardPayment() with the client secret.
});

我收到以下错误: 未处理的承诺拒绝。这个错误要么是在没有 catch 块的情况下抛出异步函数内部,要么是因为拒绝了一个没有用 .catch() 处理的承诺。

我是 promises 的新手,不确定这段代码的语法应该是什么。我可以添加

promise1.catch((error) => {
  console.error(error);
});

是的,在最后添加一个 catch 方法会捕获错误(被拒绝的 Promise)。你的建议会奏效。

var response = fetch('/secret').then(function(response) {
  return response.json();
}).then(function(responseJson) {
  var clientSecret = responseJson.client_secret;
  // Call stripe.confirmCardPayment() with the client secret.
}).catch(function(err) {
  // Handle error
});