Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't inject services into module with @Inject #30

Closed
JulianBiermann opened this issue May 5, 2017 · 3 comments
Closed

Can't inject services into module with @Inject #30

JulianBiermann opened this issue May 5, 2017 · 3 comments

Comments

@JulianBiermann
Copy link

Hi,

it looks like I'm not able to inject a service into my module when I'm using the @Inject decorator.

Following code is throwing an resolve error:

@Module({
  controllers: [
    AuthenticationController,
  ],
  components: [
    {provide: 'AuthenticationService', useClass: HttpBasicAuthenticationService},
  ],
})
export class AuthenticationModule implements NestModule {

  private readonly authenticationService: IAuthenticationService;

  constructor(
    @Inject('AuthenticationService') authenticationService: IAuthenticationService,
  ) {
    this.authenticationService = authenticationService;
  }
}

Error:

[Nest] 10638   - 2017-5-5 15:17:32   [ExceptionHandler] Nest could not resolves dependencies of AuthenticationModule.
Error: Nest could not resolves dependencies of AuthenticationModule.

The service class is decorated with the @component decorator.

@JulianBiermann
Copy link
Author

JulianBiermann commented May 5, 2017

I could track down the error to the module class.

    addCustomClass(component) {
        const { provide: metatype, useClass } = component;

        this._components.set(metatype.name, {
            name: metatype.name,
            metatype: useClass,
            instance: null,
            isResolved: false,
        });
    }

The incoming metatype is a string and does not have a name property which results in an undefined set entry.

I tested around and changed it to the following:

    addCustomClass(component) {
        const { provide: metatype, useClass } = component;
        let name = metatype.name;
        if (!name) {
            name = metatype;
        }
        this._components.set(name, {
            name: name,
            metatype: useClass,
            instance: null,
            isResolved: false,
        });
    }

This is a quick hack but it fixes the problem. Now the injector can resolve the dependency. I guess you'r using the name property if a constructor is given in the provide property of the component?

@kamilmysliwiec
Copy link
Member

Hi @JulianBiermann

Thank you for reporting. Issue is already fixed, please, update package into v. 1.0.3.

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants