I’m trying to extend a content type with an extra endpoint:
- I have created a
custom-routes.js
file (it is alphabetically at the top of the folder) in thepress-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,
},
},
];
- 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;
},
})
);
- 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