Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

730
Visualizações
How to pass constructor arguments, to a NestJS provider?

I have some Service, which requires a config object literal in its constructor, something like this:

@Injectable()
export class BatteriesService { 

    constructor(private config: Config) { }//The provider needs a config object 

}

If I simply add this class in the providers array of the module, I obviously get an error, being that a constructor argument is missing.

So, instead of just referencing the BatteriesService class, I need to somehow create an instance. I tried this:

@Module({
  controllers: [BatteriesController],
  providers: [{
    useFactory: ()=>{
     return new BatteriesService({'someProp': 'someValue'})
    },
    provide:'BatteriesService'
  }]
})

And this:

@Module({
  controllers: [BatteriesController],
  providers: [{
    useValue:new BatteriesService({'someProp': 'someValue'}),
    provide:'BatteriesService'
  }]
})

In both cases I get the following error:

Error: Nest can't resolve dependencies of the BatteriesController (?). Please make sure that the argument BatteriesService at index [0] is available in the BatteriesModule context.

How can this done, without "resorting" to bypassing the DI system, or creating another "inner" provider(config)?

over 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you need to have DI in service that has to be defined in the module.

In your case

@Injectable()
export class BatteriesService { 
  constructor(private config: ConfigService) { }
}

@Module({
  imports: [ConfigModule.forRoot({})], // Configure it as needed
  providers: [BatteriesService]
})

Your mistake is that you don't actually import ConfigModule while your service is dependent on it.

If you wish to use useFactory method then it would look like

@Module({
  providers: [{
    useFactory: (config: ConfigService) => {
      return new BatteriesService(config);
    },
    provide: BatteriesService,
    inject: [ConfigService]
  }]
})

I assumed your Config is actually Nest ConfigModule.

But if it's some custom Module you still need to import it as in the above examples.

If you want to pass an object literal as a config try this

interface MyConfigType = {
  something: string;
}
@Injectable()
export class BatteriesService { 
  constructor(@Inject('CONFIG') private config: MyConfigType) { }
}

@Module({
  providers: [{
    provide: 'CONFIG',
    useValue: {
      something: 'my-value'
    }
  }]
})
over 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Assistente virtual