Olivier Van Bulck
Olivier Van Bulck

Reputation: 786

Nest can't resolve dependencies to exported service

I'm trying to export and import a service in NestJS. It seems easy and I thought it should work like this but I got an error saying that Nest can't resolve the dependencies.

SettingsModule

This module has the service that should be imported, and exports it.

@Module({
  imports: [
    MongooseModule.forFeature([{ name: Setting.name, schema: SettingSchema }]),
  ],
  providers: [SettingsService],
  exports: [SettingsService],
})
export class SettingsModule {}

MsgraphModule

This module should import the service through the module because the service is injected in their service.

@Module({
  imports: [SettingsModule],
  providers: [MsgraphService],
})
export class MsgraphModule {}

AppModule

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost/lead-import', {
      useCreateIndex: true,
    }),
    MsgraphModule,
    SettingsModule,
    ...
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

What am I doing wrong here?

Upvotes: 2

Views: 1105

Answers (1)

Olivier Van Bulck
Olivier Van Bulck

Reputation: 786

The problem was that I used the @Inject() decorator which is only needed for custom dependency injections.

@Injectable()
export class MsgraphService {
  private client: Client;
  private authenticator;

  constructor(@Inject() private settingsService: SettingsService) {
    this.init();
    this.authenticator = new MSGraphAuthenticator();
  }

  ...
}

So removing the @Inject() did the trick.

Upvotes: 2

Related Questions