STRIPE: Cannot read property 'create' of undefined

hello everyone i am new to nodejs.
I use stripe for a payment online course project and when I use stripe in my app.js I have this result Cannot read property ‘create’ of undefined

const postCharge = (req, res) =>{
    console.log(req.body);
    const amount = 250;
  
    stripe.customers.create({
      email: req.body.stripeEmail,
      source: req.body.stripeToken
    })
    .then(customer => stripe.charges.create({
      amount,
      description: 'la pedagogie',
      currency: 'EUR',
      customer: customer.id
    }))
    .then(charge => res.render('course'));
}

Hello!

Did you log the value of stripe.charges? The error means that stripe.charges doesn’t exist, so there must be an error before or the property charges has a typo.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks for your answer

I tried everything, I removed
then (charge =>
for
.then (() =>

believer but he always shows me this error

I get it :slight_smile:.

The problem is that either stripe.customers or stripe.charges is undefined, so you need to determine which properties does srtipe actually has.

To determine that, you have to:

  • Read the documentation of the library you’re using or
  • Add a console.log(stripe) to each step (even before the first create stripe.customers.create).

If stripe is an object you created (a mongoose model perhaps?) then you should know what properties the object has, so your only option is to log the data.

If you need more help, we would need more context. Create a repl.it with your code where we can see what you’re doing :slight_smile:.

1 Like

I was able to resolve the situation, here is my code

with your help because I didn’t structure my code well

Thank you

const postCharge = (req, res) =>{
    console.log(req.body);
   
  
    stripe.customers.create({
      email: req.body.stripeEmail,
      source: req.body.stripeToken
    })
    .then(customer => stripe.charges.create({
        amount: 2500,
        currency: "usd",
        customer: customer.id
    }))
    .then(() => res.render('course))

  
  
}
1 Like