'Injected nestjs service in to CQRS is undefined

I have a service to import in CQRS but in runTime, I got an error for the service method

the service declared in constroctor and using it in excute methode

@CommandHandler(UpdateSensorsProductsCommand)
export class UpdateSensorsProductsCommandHandler implements ICommandHandler<UpdateSensorsProductsCommand>
{
  constructor(
    private eventBus: EventBus,
    private sensorProductListService: SensorProductsListService,
  ) {}

  async execute(
    command: UpdateSensorsProductsCommand,
  ) {
    // I get this error: TypeError: Cannot read property 'getAllSensorsProducts' of undefined
    this.sensorProductListService.getAllSensorsProducts()
  }
}

and I import the sensorProductListService module in my CQRS module called SystemCqrsModule

@Module({
  imports: [
    CqrsModule,
    SensorProductsListModule,
  ],
    ...
  exports: [CqrsModule],
})
export class SystemCqrsModule {}

and export sensorProductListService from sensorProductListModule

this is sensorProductListModule

@Module({
  providers: [SensorProductsListService, UnitConvert],
  exports: [SensorProductsListService],
})
export class SensorProductsListModule {}

so I have this error

TypeError: Cannot read property 'getAllSensorsProducts' of undefined

why I get this error?



Solution 1:[1]

If you are using a request scoped within a class that is injected into your service, that's not going to work.

You should either remove request scoped dependency or handle it with moduleRef. See here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vahid Najafi