Typeerror: Cannot read Property 'findOne' of Undefined (strapi)

Asked

Viewed 101 times

2

I am creating an eCommerce using Strapi, but I have a problem in one of the routes that makes the POST. Summarizing this route she opens a payment session, and for that I step one request.body with the type of product that will be purchased, the method called for this is the findOne(). But when I make the request, instead of seeing the answer, it comes undefined.

Follow the code here:

async create(ctx) {

    const {
        product
    } = ctx.request.body

    if (!product) {
        return ctx.throw(400, 'please especiefiede de product')
    }

    const realProduct = await strapi.services.product.findOne({
        id: product.id
    })
    if (!realProduct) {
        return ctx.throw(404, 'No product with such id')
    }


    const {
        user
    } = ctx.state
    const BASE_URL = ctx.request.headers.origin || 'http:localhost:3000'

    const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        costumer_email: user.email,
        mode: 'payment',
        success_url: `${BASE_URL}/success?session_id={CHECKOUT_SESSION_ID}`,
        cancel_url: BASE_URL,
        line_items: [{
                price_data: {
                    currency: 'brl',
                    product_data: {
                        name: realProduct.meProd
                    },
                    unit_amount: fromDecimalToInt(realProduct.price),
                },
                quatity: 1
            }

        ]
    })

    //aqui criar a ordem
    const newOrder = await stripe.services.order.create({
        user: user.id,
        product: realProduct.id,
        total: realProduct.price,
        status: 'unpaid',
        checkout_session: session.id

    })

    return {
        id: session.id
    }

}

What matters only goes up to the realProduct.

  • According to the error message strapi.services doesn’t have that property product. It’s very difficult to understand the context of the code without further reference. You could share the Strapi documentation link where you use this feature strapi.services, or show more of the code?

  • So, that’s the problem, the products were set above, it gets the ctx.request.body, ie it’s the data of the request that I pass. What I don’t understand is why I’m giving Undefined since the variable is already defined https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#Concept , here is the documentation on how to implement the API controller

  • Like, it’s not falling into the exception that generates me the 400 or 404,It receives the request And the context, I already have an API made in strapi, and this Function, configures a POST that starts a Payment session from the product requested in the request

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.