System Information
- Strapi 5
- PostGres
Hi,
I am trying to create a custom route to fetch an article based on its slug (defined as UUID). I am having a hard time following the tutorial here:
I created a fetch-slug.ts in the folders controllers, routes and services and I can’t even get the value of the parameters slug.
Here are my files:
//controller/fetch-slug.ts
export default {
async fetchSlug(ctx) {
try {
const data = ctx.req.params.slug;
ctx.body = {
data,
};
} catch (err) {
ctx.body = {
error: "An error occurred while fetching article from slug",
details: err instanceof Error ? err.message : "Unknown error",
};
ctx.status = 500; // Set the HTTP status code to 500 to indicate a server error
}
},
};
//routes/fetch-slug.ts
export default {
routes: [
{
method: "GET",
path: "/fetch-slug/:slug",
handler: "fetch-slug.fetchSlug",
config: {
policies: [],
middlewares: [],
},
},
],
};
Can somebody shed a light on how to read a parameter from a url (whether from the query or from req.params) and how to find an article from its slug?
Thanks all.
PS: I am currently using the endpoint:
/articles?filters[slug][$eq]=${slug}&populate=*
But as it doesn’t seem to work for localised versions for an article.