Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error 'Stripe' is not defined no-undef

I am trying to use the Stripe API to allow for payment through my website, but I am having issues adding Stripe into my project.

I used the create-react-app structure for my project, and added Stripe into the index.html file in /public/index.html as follows:

<body>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.39.0.min.js"></script>
    <script type="text/javascript" src="https://js.stripe.com/v2/"></script>

    <script>
        window.AWSCognito = window.AWS
    </script>
    <script src="https://gitcdn.xyz/repo/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js"></script>

    <div id="root"></div>
</body>

However when I test, I get an error:

/src/api/stripe.js
  2:5  error  'Stripe' is not defined  no-undef

My stripe.js file:

export const getStripeToken = (card) => new Promise((resolve, reject) => {
    Stripe.card.createToken(card, (status, {error, id}) => {
        if (error) {
            reject(error.message);
        } else {
            resolve(id);
        }
    });
});

Thanks in advance!

like image 277
Hassan Syyid Avatar asked Apr 17 '17 18:04

Hassan Syyid


2 Answers

I figured it out! I was being a bit of a dumby.

I changed Stripe to window.Stripe in my code, and it works now!

Thanks everyone.

like image 145
Hassan Syyid Avatar answered Sep 22 '22 09:09

Hassan Syyid


It's only a problem of getting it compiled. The proper way to fix it is to ask compiler to not look for Stripe and it will be available globally. Add this comment /* global Stripe */ before you initialize Stripe:

/* global Stripe */
const stripe = Stripe('STRIPE_KEY');
like image 35
Fawaz Avatar answered Sep 20 '22 09:09

Fawaz