Search code examples
cronstrapi

Why custom services do not work on cron jobs in strapi.io


I created a cron job and use Strapi custom service that I wrote. But an error comes as: TypeError: Cannot read property 'services' of undefined at Job.1 * * * * * [as job] .

Here is my cron job code:

module.exports = {
  
  '1 * * * * *': ({ strapi }) => {
    strapi.services.account.myService();
  },
};

I'm using strapi version 3.6.8.


Solution

  • The answer to this question is simple. You're using the syntax from StrapiV4 in StrapiV3. The correct syntax for cronjob in strapiv3 is as follows:

    module.exports = {
      /**
       * Simple example.
       * Every monday at 1am.
       */
    
      '0 0 1 * * 1': () => {
        // you can then reference you strapi custom service like so
        await strapi.services.account.myServiceMethod();
      },
    };
    
    References