Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DI failing to find Mikro-Orm repositories after upgrading to NestJs v8 #32

Closed
Regan-entrostat opened this issue Aug 24, 2021 · 4 comments
Assignees

Comments

@Regan-entrostat
Copy link

Regan-entrostat commented Aug 24, 2021

Describe the bug
Dependency injection errors are thrown as a result of Nest not being able to find an entity's repository. Repositories are all imported in a single module (database.module.ts) using the forFeature() function.

Stack trace

 at Injector.lookupComponentInParentModules (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/injector.js:193:19)
    at Injector.resolveComponentInstance (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/injector.js:149:33)
    at resolveParam (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/injector.js:103:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/injector.js:118:27)
    at Injector.loadInstance (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/injector.js:47:9)
    at Injector.loadProvider (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/injector.js:69:9)
    at async Promise.all (index 3)
    at InstanceLoader.createInstancesOfProviders (/home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/instance-loader.js:44:9)
    at /home/reganvanheerden/WebstormProjects/EntrostatProjects/askgogo-git-issue/backend/node_modules/@nestjs/core/injector/instance-loader.js:29:13


To Reproduce
Steps to reproduce the behavior:

  1. Open repo : git@github.com:Regan-entrostat/askgogo_issue_1.git
  2. Run 'npm install && nest start' in the backend folder

Expected behavior

  • Repository is supposed to be injected into the service.

Additional context
can get rid of this particular error by putting MikroOrmModule.forFeature([Profanity]) in the imports section of profanity.module.ts file; except this is not a good solution as the errors continue onto the next repository until I come across a repository that is used across multiple services all provided in different modules.

Versions

"@mikro-orm/cli": "^4.5.7",
"@mikro-orm/core": "^4.5.7",
"@mikro-orm/nestjs": "^4.3.0",
"@mikro-orm/postgresql": "^4.5.7",
"@mikro-orm/reflection": "^4.5.7",
"@mikro-orm/sql-highlighter": "^1.0.1",
"@nestjs/common": "^8.0.6",
"@nestjs/core": "^8.0.6",
"@nestjs/jwt": "^8.0.0",
"@nestjs/passport": "^8.0.1",
"@nestjs/platform-express": "^8.0.6",
"@nestjs/throttler": "^2.0.0",	

Node version: 14.17.5
Platform: Linux

@B4nan B4nan transferred this issue from mikro-orm/mikro-orm Aug 24, 2021
@B4nan
Copy link
Member

B4nan commented Aug 24, 2021

support for nest v8 was added in v4.3, you are using v4.2

so first try to upgrade to latest, it should be already fixed there

@Regan-entrostat
Copy link
Author

Regan-entrostat commented Aug 24, 2021

This helped resolve quite a few of the DI failures though unfortunately not all of them. Thanks very much !

@Regan-entrostat
Copy link
Author

So I am still running into the same issue as I have resented except but with different repositories. I have update the mikro-orm/nest package as suggested. The issue has been replicated and can be found in the link in the above issue. I have also adjusted the issue's stack trace and versions to reflect the changes.

@B4nan
Copy link
Member

B4nan commented Aug 24, 2021

This is actually not really about MikroORM, the issue here is that your custom repositories for some entities are not even registered, like the one for AdminUser, because the @Repository decorator does not get executed before the ORM initialization.

I will probably remove that decorator in v5, it only adds weird issues like this. It can work only if you ensure the file will get required soon enough.

So to resolve this, drop your @Repository decorators from all custom repositories, and provide them via @Entity({ customRepository: () => AdminUserRepository }) etc.

@B4nan B4nan closed this as completed Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants