nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Undefined services on scope transient

Open dany-nrich opened this issue 4 years ago • 4 comments

I have an issue when using scope transient. It prevents the injected services from being injected at all and therefore all dependencies are undefined.

I am aware of a similar issue, but the issue here is with scope transient, which unlike scope request, should not be related to http requests.

Bare-bones example:

import { Injectable, Scope } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { TestService } from '../test-service/test.service'

@Injectable({ scope: Scope.TRANSIENT })
export class MyService {

    constructor(
        private readonly httpService: HttpService,
        private readonly testServie: TestService,
    ) {}

    @RabbitSubscribe({
        exchange: 'TEST',
        routingKey: 'TEST',
        queue: 'TEST_TEST',
    })
    async main({ msg: any }: { msg: any }) {
      console.log(this.httpService); // undefined
      console.log(this.testServie); // undefined
      console.log(msg); // msg received properly
      return msg
    }
}

If no scope is given (meaning default scope is used), then everything works. This line

@Injectable({ scope: Scope.TRANSIENT })

changed to this

@Injectable()

However, it is necessary to use scope transient for this project. any suggestions for a solution?

Using nestjs 8.

dany-nrich avatar Dec 15 '21 09:12 dany-nrich

@dany-nrich We're not sure that nestjs 8 works correctly yet with our current version, that's my primary goal soon to make sure we do support it because there are really good changes Can you give us a repro? Also could you try using the same nestjs version we're using at this moment just to make sure it's not something new?

underfisk avatar Jan 12 '22 11:01 underfisk

@dany-nrich I'm reaching you to know if you have found a way around this or if Nestjs v8 helped you achieve what you're trying to? I'm closing this issue but you can re-open if its still happening and if you can provide us a repro

underfisk avatar Apr 23 '22 19:04 underfisk

I am facing the exact same issue right now

shanurrahmanpenny avatar Aug 11 '22 10:08 shanurrahmanpenny

import { Injectable, Scope } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { TestService } from '../test-service/test.service'

@Injectable()
export class MyService {

    constructor(
        private readonly httpService: HttpService,
        private readonly testServie: TestService,
    ) {}

    @RabbitSubscribe({
        exchange: 'TEST',
        routingKey: 'TEST',
        queue: 'TEST_TEST',
    })
    async main({ msg: any }: { msg: any }) {
      console.log(this.httpService); // undefined
      console.log(this.testServie); // undefined
      console.log(msg); // msg received properly
      return msg
    }
}

I am also having this issue the services that I am injecting are undefined

am not even giving any scope just @Injectable()

NathyG2524 avatar May 28 '24 18:05 NathyG2524