Following this guide, I add a new controller to override the find functionality: Controllers | Strapi Documentation Although the find function was performed and results are there, the ctx is not updated. reads status 404 and doesnt even have things like ctx.queries set. DO i need to change anything about the middleware ?
This topic has been created from a Discord post (1216051555808317450) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord
Can you share your controllers/.js code and precisely where you are inspecting the ctx object
I would have to see the controller you made to help
- I have a content type called
tarif
.
- I define the controller in
src/api/tarif/controllers/tarif.ts
and it works for client API requests. But internally it has these issues:
export default factories.createCoreController('api::tarif.tarif', ({ strapi }) => ({
async findOne (ctx, next) {
//ctx.status here is still 404 - okay
//ctx.query here is {} - why ?
const successfulFind = await super.findOne(ctx)
//ctx.status here is still 404 - why? The client gets a 200 though?
//ctx.query here is still {} - why ?
return successfulFind
},
Although in the docs found here Controllers | Strapi Documentation there is exactly that example code:
module.exports = createCoreController("api::restaurant.restaurant", ({ strapi }) => ({
async find(ctx) {
ctx.query = { ...ctx.query, locale: "en" }; // force ctx.query.locale to 'en' regardless of what was requested
const result = await super.find(ctx);
return result;
}}));
```
I don’t see what I’m doing differently. I wonder at what step/in which middleware the status
and query
is set. I do not experience this behaviour in e.g. the users permissions plugin, both query
and status
are correctly set in their respective points in their lifecycle
okay this is getting weird. Now ctx.query
is defined, but ctx.status is still 404.
strapi::responses middleware should be handeling that
thought about that. I found a workaround for now :
oh yes I did
export default factories.createCoreRouter('api::tarif.tarif', {
config: {
find: {
middlewares: [
async (ctx, next) => {
if(!ctx._ignoreHooks?.find?.before) {
await hooks.beforeFind(ctx)
}
await next();
// everything is now available from here on:
if(!ctx._ignoreHooks?.find?.after) {
await hooks.afterFind(ctx)
}
},
],
},
so one just needs to let it run the controller function and whatever comes after that. then, before the request is being returned to the client, it is all there
Did you forget to call next in your middleware lol
before i just used the controller
Still it is wired that with a controller it bugs
so now i solved the issue with a middleware. after the controller function is being run in the middleware, I’m having the proper ctx
sorry for having me implementing my own lifecycle functions but the official way is… not working for me, to say the least
I am thinking to make a new plugin.
Aka if you have a relationship and you edit it it should give you a life cycle
But I think it is worth opening a issue about the controller being wired maby