Strapi V4 create custom controller using stripe, having problem to safe in db using service.create

System Information
  • Strapi Version: 4.0.8
  • Operating System:
  • Database: MySQL
  • Node Version: 14.00
  • NPM Version:
  • Yarn Version:

Hi All! first time I write here
I am trying to follow this tutorial, but I am using v4 ( no problem with v3)

So when I try to create my custom order to use Stripe :

'use strict';

/**
 *  order controller
 */

const { createCoreController } = require('@strapi/strapi').factories;
const stripe = require("stripe")("my_test_secret_id");

module.exports = createCoreController('api::order.order', ({ strapi }) =>  ({

  async create(ctx) {
    const { address, amount, dishes, token, city, state } = JSON.parse(
      ctx.request.body
    );
    const stripeAmount = Math.floor(amount * 100);
    // charge on stripe
    const charge = await stripe.charges.create({
      // Transform cents to dollars.
      amount: stripeAmount,
      currency: "usd",
      description: `Order ${new Date()} by ${ctx.state.user._id}`,
      source: token,
    });

    // Register the order in the database
    const entity = await strapi.service('api::order.order').create({
      user: ctx.state.user.id,
      charge_id: charge.id,
      amount: stripeAmount,
      address,
      dishes,
      city,
      state,
    });

    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    return this.transformResponse(sanitizedEntity);
  },

}));

I always receive the same message

[2022-02-19 23:38:13.511] error: Cannot set property ‘publishedAt’ of undefined
TypeError: Cannot set property ‘publishedAt’ of undefined
at setPublishedAt (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/core-api/service/collection-type.js:18:32)
at Object.create (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/core-api/service/collection-type.js:62:9)
at Object.create (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/src/api/order/controllers/order.js:30:61)
at dispatch (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/koa-compose/index.js:42:32)
at returnBodyMiddleware (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:24)
at dispatch (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/koa-compose/index.js:42:32)
at policiesMiddleware (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/services/server/policy.js:24:11)
at dispatch (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/koa-compose/index.js:42:32)
at /Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:33:12
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async /Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/middlewares/body.js:24:7
at async /Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/middlewares/logger.js:22:5
at async /Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/middlewares/powered-by.js:16:5
at async cors (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@koa/cors/index.js:95:16)
at async /Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/@strapi/strapi/lib/middlewares/errors.js:13:7
at async session (/Users/apple/Desktop/projects/next-react-hooks-strapi-food-delivery/backend/node_modules/koa-session/index.js:41:7)

even though I added publistedAt in the object service

publistedAt: new Date(),

Any idea what I am missing here? I could find any information about that.

2 Likes

Hi, I finally resolved by myself , I forgot to put the params inside the data object

const entity = await strapi.service('api::order.order').create({
      data: {
      publishedAt: new Date(),
      user: ctx.state.user.id,
      charge_id: charge.id,
      amount: stripeAmount,
      address,
      dishes,
      city,
      state,
    }});
5 Likes

Thanks for publishing the solution, this error is not found much on the web.

2 Likes

thank you for solution and implementation, i new developer in the stripe with strapi jeje

1 Like

Hi, thanks @ramon_julia worked 100% for me, in my case i don’t has put the parameters inside the data object.

1 Like

Thanks for the solution, it worked for me !

1 Like

Does this tutorial you are using actually connect to stripe and create an order in stripe. I have looked at this tutorial but it only seems to create an order in strapi but not connect to stripe