#NestJS can't resolve dependencies... again...

11 messages · Page 1 of 1 (latest)

vocal orbit
#

I'm having trouble to make this work, again... This time I was certain that I checked everything, but somethings seems wrong. Any help would appreciate.

I was adding in the line 32 in the second image the userService, by adding UserModule to the ContetModule as show in image 3. ContentModule already has a forwardRef(() => UserModule), and UserModule has a line to import ContentModule like in image 4.

celest zenithBOT
#

Suggestion for @vocal orbit:
:warning: Please do not screenshot code as it causes a number of issues:

  • Ease of assistance: if someone wants to copy your code and correct it, they cannot. Making it easy for people to help you is in your best interests.
  • Editorializing: it's common to try to make images small, which means you're likely to crop out code relevant to your issue
  • Accessibility: wide images can be hard to read on mobile devices, and are impossible for screen readers.
  • Legibility: you cannot read screenshots of code directly, instead you have to open them in an enlarged context.
  • Bandwidth usage/clutter: some of our members use metered connections, and it is wasteful for them to download images of a text.

For a small amount of code, please use a code-block.

#

Please format your question or answer with Markdown formatting.
It leads to better readability and an easier time to spot problems.
For code blocks, you can wrap your block with three back ticks before and after the block, and after the first three back ticks you can add a language (like ts) to add syntax highlighting.
e.g.

```ts
@Injectable()
export class MySuperAwesomeService {
constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}

getRandomNumber(): number {
return Math.round(Math.random() * 1000);
}
}
```

Becomes :point_down:

@Injectable()
export class MySuperAwesomeService {
  constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}

  getRandomNumber(): number {
    return Math.round(Math.random() * 1000);
  }
}
glossy pivot
#

Definitely a circular import. Don't forget that you need to put forwardRef to all places participating in the circle, not just one side. That means to all @Inject decorators, as well as to all modules in imports that are part of the circle.

Use a tool like madge or dependency-cruiser to help you find circular imports and try to eliminate them.

#

The fix in this case is to put Inject(forwardRef(()=>UserService)) above the userService argument in the constructor

vocal orbit
#

I was certain that was something catchy about circular decency. Thanks for your help. Gonna try it in a few hours and return with feedback.

vocal orbit
#

it's working fine now, thx for ur help

celest zenithBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution, if you are having the same issue. If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.

mellow tiger
#

I’m using nestjs in complex project, which the provider depends on each others, the official solution can’t solve my problem. I’d implemented lose import strategy, which I think convenient

A. Create a service class only execute @onevent, import all dependency you need and define all code you wish to run. Example Msgservice.service.ts

  1. Import the service class into app.module.ts

  2. All source code which you want to execute the content in event, use eventemitter, execute your routine via emmitAsync (don’t import the Msg.service.ts class)

#

There is minor limitation, I think it is manageable compared to to fall into hell of circulation dependency

glossy pivot