NestJS inject request returning null

865 Views Asked by At

I know injecting REQUEST from cron job doesn't work, but I am having issues with injecting it from an http request - getting null.

app module:

    @Module({
    imports: [
        TypeOrmModule.forRoot(configService.getTypeOrmConfig()),
    ],
    controllers: [
        PurchaseController,
    ],
    providers: [
        ProgramsSeasonsService,
        PurchaseService,
    ],
    exports: [
        PurchaseService,
        ProductPricingService,
        ProgramsSeasonsService,
    ],
})
export class AppModule {}

controller:

@Controller('purchase')
export class PurchaseController {
    constructor(
        private purchaseService: PurchaseService) {}


    @Post('/request')
    async checkRequest() {
        this.purchaseService.checkRequest();
    }   
}

services:

@Injectable()
export class PurchaseService {
    constructor(
        @Inject(REQUEST) private request,
        private programsSeasonsService: ProgramsSeasonsService,
    ) {}
    
    checkRequest() {
        console.log('PurchaseService request: ', this.request);
        this.programsSeasonsService.checkRequest();
        
    }
}

@Injectable()
export class ProgramsSeasonsService {
    constructor(
        private spacesService: SpacesService,
        @Inject(REQUEST) private request,       
    ) {
    }

    checkRequest() {
        console.log('ProgramsSeasonsService request: ', this.request);      
    }
}

ouput log:

PurchaseService request:  IncomingMessage {
  _readableState:
   ReadableState {
     objectMode: false,
....
}
ProgramsSeasonsService request:  null   
1

There are 1 best solutions below

0
FoosMaster On

Upgrading to NestJS 8.1 (from 7.0.0) fixed the issue for me