Nest: Dependency injection is not working using `ModuleRef.create()` and `Scope.TRANSIENT`

Created on 8 May 2020  路  6Comments  路  Source: nestjs/nest

Bug Report

Current behavior

When creating an injectable class using ModuleRef.create() that is transient it works as expected:

  • The class is not being created on app start
  • A class instance gets created each time ModuleRef.create() is called

But if this class injects another class that is also transient, then it does not work as expected:

  • This class is being created on app start
  • This class is not being created each time ModuleRef.create() is called for its parent

This results in errors of undefined.

Input Code

Minimal repository to reproduce: https://github.com/MickL/nest-di-bug

await this.moduleRef.create(CatService); // Creates `CatService` but not `CatPetService`
@Injectable({scope: Scope.TRANSIENT})
export class CatService {
  constructor(
    private petService: CatPetService
  ) {}
}
@Injectable({scope: Scope.TRANSIENT})
export class CatPetService {
  constructor() {}
}

Expected behavior

  • Dont create an instance is transient services on app start
  • Create an instance of transient services and all their dependencies when ModuleRef.create() is called

Environment

  • Nest version: 7.0.9
  • Node version: 13.12.0
  • Platform: macOS 10.15.4

Workaround

@Injectable({scope: Scope.TRANSIENT})
export class CatService {
  constructor(
    private petService: CatPetService,
    private moduleRef: ModuleRef,
  ) {
    this.init();
  }

  private async init() {
    this.petService = await this.moduleRef.create(CatPetService);
  }
}
needs triage

Most helpful comment

Fixed in the latest release. Thanks for reporting!

All 6 comments

Fixed in the latest release. Thanks for reporting!

I updated to 7.0.10 but still the issue persists exactly as it was before. Could you please check? https://github.com/MickL/nest-di-bug

[Nest] 3547 - 05/12/2020, 12:38:10 PM [NestFactory] Starting Nest application...
[Nest] 3547 - 05/12/2020, 12:38:10 PM Created CatPetService
[Nest] 3547 - 05/12/2020, 12:38:10 PM [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 3547 - 05/12/2020, 12:38:10 PM addCat() called, should create a CatService and a CatPetService now...
[Nest] 3547 - 05/12/2020, 12:38:10 PM Created CatService
[Nest] 3547 - 05/12/2020, 12:38:10 PM Meow!
(node:3547) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'log' of undefined

Oh, I forgot to mention. Please, use the resolve() instead of create(). create() is used to instantiate providers that aren't a part of the module. Ill add an appropriate warning message

Thank you it is working now!

But still there seems to be an "error"? It creates an unnecessary instance on startup:
Bildschirmfoto 2020-05-12 um 13 25 33

Updated my repo.

Thanks for reporting! Please, update to the 7.0.11 and let me know if this issue still persists

Works perfectly now for .resolve(). For .create() it has the same error of not created dependencies, but this is the correct behavior? No warning message, yet.

Maybe the documentation is misleading in this case. At Instantiating classes dynamically it shows create(). I would expected this headline at Resolving scoped providers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breitsmiley picture breitsmiley  路  3Comments

menme95 picture menme95  路  3Comments

2233322 picture 2233322  路  3Comments

anyx picture anyx  路  3Comments

artaommahe picture artaommahe  路  3Comments