question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject a provider in another provider, same module

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request

I have a service/provider, let’s say it’s call ServiceA1, in a module A marked as @Injectable(). In the same module i have another service/provider, let’s say it’s call ServiceA2, marked as @Injectable(). I want to inject ServiceA1 in ServiceA2 I try with:

@Injectable()
export class serviceA2 {
  constructor( @Inject('ServiceA1') private readonly serviceA1: ServiceA1){
  }
  
myFunction(integer A):  {
    const something = this.serviceA1.functionInServiceA1();
  }

}

This code throw an error: serviceA1 undefined.

My module.ts

@Module({
  imports: [],
  controllers: [],
  providers: [ServiceA1, ServiceA2]
})

I have to make another module to use serviceA2 in serviceA1?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ale958commented, Oct 31, 2018

Providers do not depend on each other, only serviceA2 depends(needs) on ServiceA1, so I think there are other solutions to using that forwardRef

1reaction
trumbittacommented, Jul 17, 2019

@adrien2p I ended up splitting the module into two global modules. That effectively worked around the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inject a provider in another provider, same module #1250
In the same module I have another service/provider, let's say it's call ServiceA2 , marked as @Injectable() . I want to inject ServiceA1...
Read more >
Custom providers | NestJS - A progressive Node.js framework
The 'CONNECTION' custom provider uses a string-valued token. Let's see how to inject such a provider. To do so, we use the @Inject()...
Read more >
Providing dependencies in modules - Angular
A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency. Most of the time,...
Read more >
Inject Service From Another Module in NestJS (2022)
Let's create two modules (plus their controller and service) by NestJS CLI. The CLI creates these modules for you while adding them to...
Read more >
Providers Within Modules - Configuration Language | Terraform
A module intended to be called by one or more other modules must not contain any provider blocks. A module containing its own...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found