Cannot create custom route - controller in handler parameter is undefined

I’m trying to extend a content type with an extra endpoint:

  1. I have created a custom-routes.js file (it is alphabetically at the top of the folder) in the press-releases/routes folder (press-releases is a content type that was generated through the UI) with the following code inside:
"use strict";

module.exports = [
  {
    method: "GET",
    path: "/press-releases/years",
    handler: "press-release.getYears",
    config: {
      auth: false,
    },
  },
];
  1. I have extended the core press-release controller to have a getYears() method:
module.exports = createCoreController(
  "api::press-release.press-release",
  ({ strapi }) => ({
    async getYears(ctx) {
      console.log("wtf is hapenning");
      const test = await strapi
        .service("api::press-release.press-release")
        .getYears();

      return test;
    },
  })
);
  1. I have extended the press-release service with the getYears() method:
module.exports = createCoreService(
  "api::press-release.press-release",
  ({ strapi }) => ({
    async getYears() {
      return { message: "test" };
    },
  })
);

When I try to run Strapi I get:
TypeError: Error creating endpoint GET /press-releases/years: Cannot read properties of undefined (reading 'getYears') at getAction (/opt/node_modules/@strapi/strapi/dist/services/server/compose-endpoint.js:104:24)
I seem to be doing everything by the book, yet it’s not working. I tried changing the handler parameter to api::press-release.press-release.getYears due to someone on the forums writing it that way - that removes the error and Strapi starts just fine, but when I request the route from http://127.0.0.1:1337/api/press-releases/years I get 404. I tried logging to the console in the getYears() method in the controller, but when I request the endpoint, nothing pops up in the console. Very confused.

This topic has been created from a Discord post (1267872687951446136) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

Hi! I had the same problem, some days ago. My problem was that I didn’t include the service/controller into the index file, maybe it’s the same error, I hope it helps you.

Sorry for my English.

That didn’t fix it for me. From what I’ve gathered we shouldn’t need index files either way, because the file are imported automatically by Strapi.

I was able to circumvent the issue by using this solution found on the forums:

const defaultRouter = createCoreRouter("api::press-release.press-release");

const customRouter = (innerRouter, extraRoutes = []) => {
  let routes;
  return {
    get prefix() {
      return innerRouter.prefix;
    },
    get routes() {
      if (!routes) routes = extraRoutes.concat(innerRouter.routes);
      return routes;
    },
  };
};

const myExtraRoutes = [
  {
    method: "GET",
    path: "/press-releases/years",
    handler: "api::press-release.press-release.getYears",
  },
];

module.exports = customRouter(defaultRouter, myExtraRoutes);