Search code examples
javascriptnode.jsstripe-paymentsunhandled-promise-rejection

Unhandled promise rejection stripe paymentIntent example


I'm trying to setup a stripe payment app using node and express, following the example here: https://stripe.com/docs/payments/accept-a-payment#web

I created the route in my server side app code as indicated, and inserted the client-side code in my html file. I'm trying to create the app without a template engine, just 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.
});

I'm getting the following error: 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().

I'm new to promises and not sure what the syntax with this code should be. Can I add

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

Solution

  • Yes, adding a catch method at the end would catch the error(rejected Promise). What you suggested would work.

    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
    });