How can I call an async controller-function in another controller (Strapi v4)? 🤔

  • Strapi Version 4:

Hello Guys,

I have a easy question. :thinking:

For example: I have a ContentType called “cat”:

module.exports = createCoreController('api::cat.cat', ({ strapi }) =>  ({
...
const res = await super.find();
...
};

How do I call “find()” in “cat” from another controller like:

module.exports = createCoreController('api::dog.dog', ({ strapi }) =>  ({
...
// I want to find some cats... :);
...
};

Thanks in advance!

I can find the answer to my first question myself, but I still have another question. :thinking: e.g.:

module.exports = createCoreController('api::cat.cat', ({ strapi }) =>  ({
...
  // Method 1: Creating an entirely custom action
  async exampleAction(ctx) {
    try {
      ctx.body = 'ok';
    } catch (err) {
      ctx.body = err;
    }
  },
};

How do I call “exampleAction(ctx)” in “cat” from “dog” controller?

module.exports = createCoreController('api::dog.dog', ({ strapi }) =>  ({
...
// How do I call “exampleAction(ctx)” in “cat” from "dog"?
...
};

strapi.controllers["api::cat.cat"].exampleAction

1 Like

Many thanks for the quick response! :+1: :beer: