Extending core controllers does not work when create

System Information
  • Strapi Version: 4.2.2
  • Operating System: Ubuntu 20
  • Database: SQlite
  • Node Version: 14.16
  • NPM Version: 6.14.11
  • Yarn Version: X

Hello,

Today I’ve wanted to customize the default controller for my content-type but without success. I have a content type named “notification”, so in src/api/notification/controllers/notification.js I’ve added :

"use strict";

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController(
  "api::notification.notification",
  ({ strapi }) => ({
    async find(ctx) {
      const { data, meta } = await super.find(ctx);

      console.log('in find');

      return { data, meta };
    },
  })
);

When calling /api/notifications I can see the “in find” log, so it’s working. But I want to make this when creating a notification entry, I tried this :

// src/api/notification/controllers/notification.js

"use strict";

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController(
  "api::notification.notification",
  ({ strapi }) => ({
    async create(ctx) {
      console.log('in creation')
      const response = await super.create(ctx);

      return response;
    }
  })
);

Following : Backend customization - Controllers - Strapi Developer Docs

And when creating a new notification entry from backend (not from API) I never see the “in creation” log… I’ve searched also about webhooks and other things but it do not meet my requirements or not easy to implement.

How to make the “in creation” seen in log?

Thank you for you help.

Hello I have the same problem, did you find some solution?

I know that my extension works, 'cause when I start the proyect I can see this log

export default factories.createCoreController(
  "api::order.order",
  ({ strapi }) => {
    strapi.log.debug("Extended controller registred ");

    return ({
      /* Creating a new order */
  
      async create(ctx) {
        strapi.log.debug("I cant see this log and therefore a can do any logic");
      },
    })
  }
);

You need to create the notification using REST API , i.e create it from swagger OR postman API and it’ll hit the Create controller ,
i think you’re probably trying to test using admin panel ( this will not work )

So try to HIT the endpoint

1 Like