Marcos J.C Kichel
Marcos J.C Kichel

Reputation: 7219

NestJS: Injected dependencies are undefined during cron job execution

I cannot get the contextual reference of the MyOtherService.

Following the docs, this is my code:

@Injectable()
export class MyService {
    constructor(private readonly myOtherService: MyOtherService) {}

    @Cron(CronExpression.EVERY_10_SECONDS)
    async test() {
        this.myOtherService.someMethod()  //myOtherService is undefined;
    }
}

Upvotes: 0

Views: 2774

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70131

It sounds like you have a dependency using the REQUEST scope, which cannot be instantiated during CRON as there is no request to work with. Everything must be the singleton (DEFAULT) scope.

Upvotes: 4

Related Questions