Services - Call local functions inside module.exports

Hello,

Lets say I have two service functions, init() and storeData():

module.exports = {
  init: async () => {
    let { data } = await axios('http://getdata.com');
    strapi.plugins['scraper-plugin-test'].services.scraper.storeData(data); //Here I want to call the storeData() local function.
  },

  storeData: async (data) => {
      console.log('Store data here:', data);
  }
}

Is it possible to call storeData() inside init() function by using some shorthand metod? Something like: this.storeData(data), as always writing the full function stack strapi.plugins['scraper-plugin-test'].services.scraper.storeData() is kinda exhausting.

I know its not related to Strapi, but I’m interested how others deal with it? As I have some services that have 20 micro-functions inside it. And always writing the full path with "strapi.plugins[].services...." drives me nuts.

Thanks

Hi, you can by doing like this

module.exports = {
  async init(){
    let { data } = this.storeData()
  },

  async storeData (data) {
      console.log('Store data here:', data);
  }
}

Note: Don’t use arrow functions if you want to use “this”