System Information
- Strapi Version: 4.8.2
- Operating System: Mac Ventura 13.0.1
- Database: default
- Node Version: v18.12.1
- NPM Version: -
- Yarn Version: 1.22.19
I’d like to add a custom api endpoint. For this I have followed the steps as described in the documentation.
I have added a file called 01-custom-route.ts
in the /src/api/<existingContentType>/routes
(next to the already existing existingContentType.js
file. It is unclear to me why it generates js files in my ts project, but that is not the subject of the issue.
The problem is that I get an error when I run yarn develop
as soon as I add the sample code from the documentation into the 01-custom-route.ts
file
export default {
routes: [
{ // Path defined with a URL parameter
method: 'GET',
path: '/restaurants/:category/:id',
handler: 'Restaurant.findOneByCategory',
},
{ // Path defined with a regular expression
method: 'GET',
path: '/restaurants/:region(\\d{2}|\\d{3})/:id', // Only match when the first parameter contains 2 or 3 digits.
handler: 'Restaurant.findOneByRegion',
}
]
}
error: Cannot read properties of undefined (reading ‘forEach’)
TypeError: Cannot read properties of undefined (reading ‘forEach’)
at /(…)/node_modules/@strapi/strapi/lib/services/server/register-routes.js:98:21
Placing a console.log at line 97 of register-routes.js, with the routes
object. I get the following result:
{ prefix: [Getter], routes: [Getter] }
{ prefix: [Getter], routes: [Getter] }
{ }
I assume that the first two are from a different content type/api. But the third one is what is causing the problem. Removing the 01-custom-route.ts
will remove the empty object, and show some more lines like the first two.
Why is this happening? What am I missing here?