moahammad daneshjoo
moahammad daneshjoo

Reputation: 115

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?

Upvotes: 1

Views: 788

Answers (1)

Vahid Najafi
Vahid Najafi

Reputation: 5263

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

Upvotes: 3

Related Questions