The error that says that the dependency can't be resolved is not clear enough. Currently, the error output says:
[ExceptionHandler] Nest can't resolve dependencies of the LeadService (LeadRepository, ?). Please make sure that the argument dependency at index [1] is available in the LeadModule context.
From this output, I can conclude that the dependency ConsentService
of my LeadService
. See LeadService
constructor below.
Additionally, the output also puts the following suggestion:
Potential solutions:
- If dependency is a provider, is it part of the current LeadModule?
My answer: It is a provider, but it's not part of the current module. It's a provider from the ConsentModule. See ConsentModule definition.
- If dependency is exported from a separate @Module, is that module imported within LeadModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
My answer: Yes, it is exported from the ConsentModule and it is imported in the LeadModule thus I don't understand why is this failing.
Input Code
ConsentService
@Injectable()
export class ConsentService {
constructor(@InjectRepository(Consent) private repository: Repository<Consent>) {}
}
LeadService
@Injectable()
export class LeadService<T extends LeadPayload> {
constructor(
@InjectRepository(Lead)
private leadRepository: Repository<Lead>,
@Inject()
private consentService: ConsentService
) {}
}
ConsentModule
import { Module } from '@nestjs/common';
import { ConsentService } from './consent.service';
import { Consent } from '../db/models';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([Consent])],
providers: [ConsentService],
exports: [ConsentService]
})
export class ConsentModule {}
LeadModule
import { Module } from '@nestjs/common';
import { LeadService } from './lead.service';
import { Lead } from '../db/models';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConsentModule } from './consent.module';
@Module({
imports: [ConsentModule, TypeOrmModule.forFeature([Lead])],
providers: [LeadService],
exports: [LeadService]
})
export class LeadModule {}
AppModule
@Global()
@Module({
imports: [
ConsentModule,
LeadModule,
TypeOrmModule.forRoot({
...getDbConnectionProperties(),
entities: [Consent, Lead]
})
],
controllers: [
DevController,
HealthController
],
providers: []
})
export class AppModule {}
I would like to know why exactly is the error happening because I think I have declared everything correctly