I have two questions. 1.How to run cron job locally in loopback framework? 2.How to inject a class in a service file and then use the class's method?
For example I have code like
import {CronController} from "../CronController" ;
import cron from "node-cron";
export class LiveUpdate() {
constructor(protected cronController: CronController){}
async start(){
this.cronJob()
}
private async cronJob(){
cron.schedule("59 23 * * *",async () => {
//... calls function here
});
}
}
Now consider I have a service class, MyService.
And I want to call the start() function of LiveUpdate in this service class. How do I inject it, so I can use the start() function in this class?
I have not tried anything as I was unable to find anything.
In your service, you will import the LiveUpdate and the CronController then, you must create a property called cronController CronController type, and create an instance in the constructor for that property and you must pass the context of your application using "this"...
Last thing, you must create an instance of LiveUpdate and past the property of cronControlle based on your code example you are receiving a cronController in your constructor, so, after that, you just can call the start method using await and that works perfectly
in the first line "route/yourfilename" means the route of the file you shared (just to be clear) hope its help