Create custom service Cannot read property 'kind' of undefined on v4

Hello guys im creating a custom service on the new v4 strapi following the docs this is my code:

const { createCoreService } = require('@strapi/strapi').factories;

// createPlan code ommited for simplicity


module.exports = createCoreService('api::payments.payments', ({ strapi }) =>  ({     
    async createPlan(config){

        console.log(config)
        let localPlan = await createStripePlan(config, false);
        let globalPlan = await createStripePlan(config, true);

        return {
            localPlan, 
            globalPlan,
        }
    }
}))

Im trying to use this service on a lifecycle as follow:

module.exports = {


    async beforeCreate (event) {
        

        let plan = event.params.data 

        let planConfig =  {

            name: plan.name, 
            basePrice: plan.basePrice, 
            usdPrice: plan.usdPrice,
            products: plan.productsQuota,
            orders: plan.ordersQuota, 
            users: plan.usersQuota, 
            channels: plan.channelsQuota            

        }

        let  products = await strapi.service("api::payments.payments").createPlan(planConfig)

        if(products){
            event.params.data.stripeLocalProductId = products.localPlan.id
            event.params.data.stripeGlobalProductId = products.globalPlan.id

        }
    },
    async beforeUpdate (event) {
        let plan = event.params.data 

        let planConfig =  {

            name: plan.name, 
            basePrice: plan.basePrice, 
            usdPrice: plan.usdPrice,
            products: plan.productsQuota,
            orders: plan.ordersQuota, 
            users: plan.usersQuota, 
            channels: plan.channelsQuota            

        }

        let  products = await strapi.service("api::payments.payments").createPlan(planConfig)

        if(products){
            event.params.data.stripeLocalProductId = products.localPlan.id
            event.params.data.stripeGlobalProductId = products.globalPlan.id

        }
        
        

    }

}

When I save the object on the admin dashboard triggering the lifecycle method I have seen this error on the console.

error: Cannot read property ‘kind’ of undefined
0|fenicia-strapi | TypeError: Cannot read property ‘kind’ of undefined
0|fenicia-strapi | at isSingleType (/srv/fenicia-strapi/node_modules/@strapi/utils/lib/content-types.js:90:25)
0|fenicia-strapi | at createService (/srv/fenicia-strapi/node_modules/@strapi/strapi/lib/core-api/service/index.js:21:7)
0|fenicia-strapi | at /srv/fenicia-strapi/node_modules/@strapi/strapi/lib/factories.js:30:25
0|fenicia-strapi | at Object.get (/srv/fenicia-strapi/node_modules/@strapi/strapi/lib/core/registries/services.js:36:69)
0|fenicia-strapi | at Strapi.service (/srv/fenicia-strapi/node_modules/@strapi/strapi/lib/Strapi.js:95:43)
0|fenicia-strapi | at Object.beforeUpdate (/srv/fenicia-strapi/src/api/plan/content-types/plan/lifecycles.js:45:38)
0|fenicia-strapi | at modelsLifecyclesSubscriber (/srv/fenicia-strapi/node_modules/@strapi/database/lib/lifecycles/subscribers/models-lifecycles.js:15:41)
0|fenicia-strapi | at Object.run (/srv/fenicia-strapi/node_modules/@strapi/database/lib/lifecycles/index.js:47:17)
0|fenicia-strapi | at processTicksAndRejections (internal/process/task_queues.js:97:5)
0|fenicia-strapi | at async Object.update (/srv/fenicia-strapi/node_modules/@strapi/database/lib/entity-manager.js:217:7)
0|fenicia-strapi | at async Object.update (/srv/fenicia-strapi/node_modules/@strapi/strapi/lib/services/entity-service/index.js:219:18)
0|fenicia-strapi | at async Object. (/srv/fenicia-strapi/node_modules/@strapi/strapi/lib/services/entity-service/index.js:67:20)
0|fenicia-strapi | at async Object.update (/srv/fenicia-strapi/node_modules/@strapi/plugin-i18n/server/services/entity-service-decorator.js:142:19)
0|fenicia-strapi | at async Object.update (/srv/fenicia-strapi/node_modules/@strapi/plugin-content-manager/server/controllers/collection-types.js:123:27)
0|fenicia-strapi | at async returnBodyMiddleware (/srv/fenicia-strapi/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:18)
0|fenicia-strapi | at async module.exports (/srv/fenicia-strapi/node_modules/@strapi/plugin-content-manager/server/middlewares/routing.js:39:3)

For me it is happening on the service factory but i don’t know what is happening

Did I missed something? Is the service correctly created?

System Information
  • Strapi Version: v4
  • Operating System: ubuntu
  • Database: mysql RDS
  • Node Version:
  • NPM Version:
  • Yarn Version:

1 Like

I got a similar error because I did not fully follow the migration guide. To summarize: take app offline, commit checkpoint, delete node modules, change the config.js to update all the fields to the latest version, yarn install, yarn build, yarn develop. I know probably did this following the guide, but it helped me.

Also having the same issue on V 4.1.10 which was migrated from V 4.1.8 following the migration guide. Tried this on a fresh install of V 4.1.11 and facing the same issue.

when I run yarn strapi services:list, I can see my file recognised as a service.

Same issue here but in a new project v4.1.11

I faced the same problem today. It seems that calling createCoreService() requires that content-types of the same name to exist. In my case, create that content-types fixed the problem.

strapi generate
# Choose "content-type"
# Content type singular name: payments
# Do you want to add attributes? No
# Add model to an existing API: payments

You can check if content-types exist by either in JS:

strapi.contentType("api:payments.payments");

Or command line:

strapi content-types:list
2 Likes

This workaround did it for me, thanks for the fix.

Seems like the original implementation is now broken for some users and the workaround uses a content type.

Thanks! That was frustrating me, couldn’t find any possible errors. This behavior is also weird cause sometimes one might just want to execute some business logic on a request and thus not need content-types to be created at all. Should this be fixed?

1 Like