davidroman0O / moleculer-cron

Moleculer Addons - Cron tasks
MIT License
38 stars 25 forks source link

How can I call methods? #9

Closed BrunyC closed 2 years ago

BrunyC commented 2 years ago

I created methods but when I call inside action I receive an error:

TypeError: this.myMethod is not a function

code:

crons: [
 {
    name: "Cron-Job",
    cronTime: process.env.CRON_TIMER,
    onTick: function() {
     this.getLocalService("RefreshToken").actions.mainAction();
    },
    manualStart: false
 },
],
actions: {
 mainAction: {
    async handler() {
          const result = this.myMethod();
      // do something with result;
    },
 },
},
methods: {
  myMethod() {
   return "something"; 
  }
}

What is missing?

davidroman0O commented 2 years ago

This is a Javascript misunderstanding, not related to this lib or moleculer https://medium.com/tech-tajawal/javascript-this-4-rules-7354abdb274c