I am having trouble getting my Controller to call a core find
method for my fixture
api. I followed the documentation correctly.
module.exports = createCoreController('api::fixture.fixture', ({ strapi }) => ({
async find(...args) {
console.log("hitting custom service")
return await strapi.service('api::fixture.fixture').find(args);
}
}))
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = createCoreService('api::fixture.fixture'), ({ strapi }) => ({
async find(...args) {
console.log("hit me!")
const {results, pagination} = await super.find(...args);
return {results, pagination};
},
});
After hitting http://localhost:1337/api/fixtures?populate=*
, it does not interact with the “hit me!” log i created, therefore this service is not being invoked at all.
Any ideas?
This topic has been created from a Discord post (1222182438961418261) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord
Because you are over riding an existing service, you would construct your controller like this
const { createCoreController } = require(“@strapi/strapi”).factories;
module.exports = createCoreController(“api::fixture.fixture”, ({ strapi }) => ({
async find(ctx) {
// your custom logic for modifying the input
ctx.query = { …ctx.query, locale: “en” }; // force ctx.query.locale to ‘en’ regardless of what was requested
// Call the default parent controller action
const result = await super.find(ctx);
return result;
},
}));
And you service will look like the following.
"use strict";
/**
* fixture service
*/
const { createCoreService } = require("@strapi/strapi").factories;
module.exports = createCoreService("api::fixture.fixture", ({ strapi }) => ({
async find(...args) {
console.log("hit me!");
const { results, pagination } = await super.find(...args);
return { results, pagination };
},
}));
here is the repo of the working example. I used the following documentation Services | Strapi Documentation
repo: testing-service-and-controller-overide/src/api/fixture at main · PaulBratslavsky/testing-service-and-controller-overide · GitHub
This worked. And I go the console log.
Thanks very much Paul, so the controller was wrong I guess! Works a treat now 