Nest can't resolve dependencies of the UserService (?, +). Please make sure that the argument at index [0] is available in the current context
Asked Answered
E

3

13

having some problems, di/circular-references I'm sure I'm doing wrong, I just can't see it.

Any help would be much appreciated

user.module.ts

@Module({
    imports: [
        TypeOrmModule.forFeature([User]),
        forwardRef(() => AuthModule)
    ],
    providers: [ UserService, TokenService ],
    exports: [ UserService ]
})
export class UserModule {}

auth.module.ts

@Module({
    imports: [ forwardRef(() => UserModule) ],
    controllers: [ AuthController ],
    providers: [
        AuthService,
        UserService,
        TokenService
    ],
    exports: [ AuthService, TokenService ]
})
export class AuthModule {}

app.module.ts

@Module({
  imports: [
    TypeOrmModule.forRoot(),
    forwardRef(() => UserModule),
    forwardRef(() => AuthModule),
  ],
})
export class AppModule {}

I get [ExceptionHandler] Cannot read property 'module' of undefined.

Was originally having "Nest can't resolve dependencies of the UserService. I then deleted the UserModule entirely and just used the AuthModule, everything worked, then decided to add the UserModule back in today and move the code from AuthModule back into the UserModule, then discovered the forwardRef(() => ) and now I'm getting the cannot read property 'model'.

Thanks in advance

Ell answered 5/8, 2018 at 8:59 Comment(3)
Could you include the imports part of your code?Hackneyed
Why UserService is defined in the AuthModule?Incise
I've tried different things, that's the state that I ended up with a different error.Ell
C
29

You need to inject the (circular) dependencies of the UserService via forwardRef as well, see the CommonService example:

constructor(@Inject(forwardRef(() => TokenService)) private readonly tokenService: TokenService) {}

Also, providers should only be declared in one module. If you want to use them in another module, export those providers and then just import the module: UserModule imports AuthModule and does not provide the AuthService again. This way all exported providers can be used in the UserModule.

So, remove the UserService from the AuthModule's providers list and the TokenService from the UserModule.

Crayfish answered 5/8, 2018 at 12:30 Comment(2)
In the docs they pointed to use one and not them combinedFunctionary
I also had to use both, which is weirdSabayon
N
4

In case, you got above error when adding the repository to the service with TypeORM, then add below code in the module file

imports: [
  TypeOrmModule.forFeature([<your-entity>])
],
Norbert answered 16/5, 2020 at 6:32 Comment(0)
P
-1

I got error when I have 2 consecutive commas in providers of my array

  providers: [
    AService,
    BService,, // <- NOT ALLOWED CIRCULAR PROBLEM 
  ],
Pyrexia answered 9/8, 2020 at 15:11 Comment(1)
You can use tools like prettier, eslint/tslint to avoid thisSeptuagint

© 2022 - 2024 — McMap. All rights reserved.