未处理的承诺拒绝条带付款意图示例

时间:2020-07-26 17:18:46

标签: javascript node.js stripe-payments unhandled-promise-rejection

我尝试按照此处的示例使用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()处理的诺言而引起的。

我是Promise的新手,不知道此代码的语法应该是什么。我可以添加

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

1 个答案:

答案 0 :(得分:1)

是的,在末尾添加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
});