System Information
- Strapi Version: 4.15.5
- Operating System: macOs
- Database: Sqlite
- Node Version: v18.12.1
- NPM Version: 9.2.0
- Yarn Version: 1.22.19
Following documentation I’m trying to extend controller like this:
import { factories } from "@strapi/strapi";
console.log("createCoreController");
export default factories.createCoreController("api::revision.revision", () => ({
async create(ctx) {
const { id } = ctx.state.user;
console.log({ id });
(ctx.request.body as any).data.author = id;
const entity = await strapi.entityService.create(
"api::revision.revision",
ctx.request.body
);
return entity;
},
}));
But without luck. Seems strapi is ignoring it. Using API I can create a record, but without user id provided. console.log also does not show anything neither the first one with string “createCoreController”, nor with id.
What am I doing wrong?
Where are you making this change. Which file are you in?
To extend the controller you have to make sure it is following this temlate.
./src/api/restaurant/controllers/restaurant.js
const { createCoreController } = require('@strapi/strapi').factories;
module.exports = createCoreController('api::restaurant.restaurant', ({ strapi }) => ({
async create(ctx) {
// validateQuery (optional)
// to throw an error on query params that are invalid or the user does not have access to
await this.validateQuery(ctx);
// sanitizeQuery to remove any query params that are invalid or the user does not have access to
// It is strongly recommended to use sanitizeQuery even if validateQuery is used
const sanitizedQueryParams = await this.sanitizeQuery(ctx);
const { results, pagination } = await strapi.service('api::restaurant.restaurant').find(sanitizedQueryParams);
const sanitizedResults = await this.sanitizeOutput(results, ctx);
return this.transformResponse(sanitizedResults, { pagination });
}
}));
Notice how they are ./src/api/restaurant/controllers/restaurant.js
using the collections existing file. Not sure if this is what you are doing.
Thank you for you answer. The file is correct. In my case it is src/api/revision/controllers/revision.ts
. The template you provided in general is the same as mine, but strapi ignores it.