Error when trying to create a simple custom route: Error creating endpoint GET<route>: Cannot read property <controller function> of undefined

System Information
  • Strapi Version: V4
  • Operating System: aws linux AMI
  • Database: mysql
  • Node Version: 14.18.1
  • NPM Version: 6.14.15

I am trying to get the example code from the docs “Development/Back-end customization” to run for a simple custom route “/api/customizeUser/…”.

My folder structure:
folderstructure

My files content:

// api/customizeUser/routes/customizeUser
module.exports = {
  routes: [
    { // Path defined with a URL parameter
      method: 'GET',
      path: '/customizeUser',
      handler: 'customizeUser.index',
      // }
    }
  ]
}

// api/customizeUser/controllers/customizeUser
module.exports = {
  async index(ctx, next) {
    ctx.body = 'Hello World!'; // we could also send a JSON
  },
};

When i try to start the server with npm run develop the server crashes with the following error:

error: Error creating endpoint GET /customizeUser: Cannot read property ‘index’ of undefined
TypeError: Error creating endpoint GET /customizeUser: Cannot read property ‘index’ of undefined

What am i missing?

Hey there @dmuellner , did you ever figure out the answer to this question?

I ran into this and discovered that you can’t use camel-casing, like “customizeUser”. It has to be “customize-user”. The generate command line tool really should restrict this at the point of creation, since it already restricts other characters like “_”.

8 Likes

Excellent, you just saved me lots of time! This was indeed the issue!

I renamed my controller js file from customControlle.js to custom-controller.js and it worked like a charm! :sparkles:

Strapi V4 really needs to document this thing. I had been scratching my head regarding this for quite some time today and I looked everywhere but couldn’t find a place where Strapi v4 docs explicitly mention the casing to be kebab-case instead of camelCase.

1 Like

I had a similar problem and it was not related to the camelCase. That’s why I thought to put it here thinking that it might be helpful for someone in future.

In my case, accidentally i was doing array destructuring

const [ licenseKey ] = ctx.request.body;

instead of object descrtucuring

const { licenseKey } = ctx.request.body;

Happy debugging :slight_smile: