Nest: Inject a provider in another provider, same module

Created on 31 Oct 2018  路  10Comments  路  Source: nestjs/nest

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?

All 10 comments

If both providers depend upon each other, you create a so called "circular dependency"
For this you'll have to use forwardRef

OBS: Also you shouldn't provide a string in the Inject decorator, but the actual class reference instead.

app.module.ts

import { Module, forwardRef } from '@nestjs/common';

import { ServiceA1 } from './a1.service';
import { ServiceA2 } from './a2.service';


@Module({
  providers: [
    forwardRef(() => ServiceA1),
    forwardRef(() => ServiceA2),
  ],
})
export class AppModule {}

a1.service.ts

import { Injectable, forwardRef } from '@nestjs/common';

import { ServiceA2 } from './a2.service';

@Injectable()
export class ServiceA1 {
  constructor(
    @Inject(forwardRef(() => ServiceA2)) 
    private readonly serviceA2: ServiceA2,
  ){}
}

a2.service.ts

import { Injectable, forwardRef } from '@nestjs/common';

import { ServiceA1 } from './a1.service';

@Injectable()
export class ServiceA2 {
  constructor(
    @Inject(forwardRef(() => ServiceA1)) 
    private readonly serviceA1: ServiceA1,
  ){}
}

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

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

You don't. I would suggest creating appropriate issue on the StackOverflow with somewhat more details about your problem.

done :)

@ale958 @adrien2p did you find a solution? I'm having the same problem.

@marcus-sa

Type 'ForwardReference<any>' is not assignable to type 'Provider'.
  Type 'ForwardReference<any>' is missing the following properties from type 'FactoryProvider': provide, useFactory

My problem was related to DialogFlowModule lib which I was using, maybe the stackoverflow discussion https://stackoverflow.com/questions/53088444/inject-a-provider-in-another-provider-same-module-1250 will give you some help

@trumbitta could you share a bit of code please ?

The dialogFlowModule was fixed, maybe tour problem come from elsewhere

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

menme95 picture menme95  路  3Comments

FranciZ picture FranciZ  路  3Comments

thohoh picture thohoh  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

mishelashala picture mishelashala  路  3Comments