TS中的typeModule.forfeature()的参数应该是什么?
我正在尝试将存储库添加到我的typeormmodule.forfeature()中,据我了解,它定义了创建了哪些存储库。
以下文档,我看到我们可以简单地添加实体名称,例如: imports:[typeormmodule.forfeature([user])],
在另一个教程中,我看到有人将存储库名称放在forfeature中,例如:
imports:[typemormmodule.forfeature UserRepository])],
在我当前的代码中,我声明了很少的实体和存储库。使用DataSource创建存储库,例如:
导出const wheelrepository =(src:dataSource)=> src.getRepository(wheel);
但是,我可以使代码工作的唯一方法就是这样,这是上面两个解决方案的混合 - 而我所有的实体声明都与上面提到的那样严格相似: (这是一个car.module.ts)
@Module({
imports: [TypeOrmModule.forFeature([Car, Wheel, OptionRepository])], /* define repositories */
controllers: [CarController, WheelController, OptionController],
providers: [CarService, WheelService, OptionService],
})
export class CarModule {}
...因此,通过为我的实体明确命名“存储库”。
虽然这不起作用:
@Module({
imports: [TypeOrmModule.forFeature([Car, Wheel, Option])], /* define repositories */
controllers: [CarController, WheelController, OptionController],
providers: [CarService, WheelService, OptionService],
})
export class CarModule {}
完成输出:
[Nest] 18832 - 2022-06-22 16:34:13 ERROR [ExceptionHandler] Nest can't resolve dependencies of the OptionService (?). Please make sure that the argument OptionRepositoryRepository at index [0] is available in the CarModule context.
Potential solutions:
- If OptionRepositoryRepository is a provider, is it part of the current CarModule?
- If OptionRepositoryRepository is exported from a separate @Module, is that module imported within CarModule?
@Module({
imports: [ /* the Module containing OptionRepositoryRepository */ ]
})
Error: Nest can't resolve dependencies of the OptionService (?). Please make sure that the argument OptionRepositoryRepository at index [0] is available in the CarModule context.
Potential solutions:
- If OptionRepositoryRepository is a provider, is it part of the current CarModule?
- If OptionRepositoryRepository is exported from a separate @Module, is that module imported within CarModule?
@Module({
imports: [ /* the Module containing OptionRepositoryRepository */ ]
})
at Injector.lookupComponentInParentModules (/Users/me/Documents/project_test/node_modules/@nestjs/core/injector/injector.js:231:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `nest start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
当我尝试通过“导入列表”中的“轮子”更改“轮子”时,我会有相同的错误。
我不明白在哪种情况下我应该能够使用一种或另一种方法,为什么在这种情况下,我必须同时混合两者。
[编辑]
我通过比较文件找到了原因:在我的optionservice构造函数中,我注入@InjectRepository(optionRepository),而不是@InjectRepository(option)。 我仍然很好奇知道为什么会发生这种错误,因为我不了解汽车模块之间的链接,就像“上方”和服务中的注入。
I am trying to add repositories to my TypeOrmModule.forFeature(), which from what I understood, defines which repositories have been created.
Following the documentation, I saw that we could simply add the entity name, like :imports: [TypeOrmModule.forFeature([User])],
In another tutorial, I saw someone putting direcly the repository name into forFeature, such as follow :
imports: [TypeOrmModule.forFeature([UserRepository])],
In my current code, I am declaring few entities and repositories. Repositories are created using DataSource, for example :
export const WheelRepository = (src: DataSource) => src.getRepository(Wheel);
However, the only way I could make my code works is this way, which is a mix of two solutions above - while all my entities declarations are strictly similar as the one mentionned above :
(here is a car.module.ts)
@Module({
imports: [TypeOrmModule.forFeature([Car, Wheel, OptionRepository])], /* define repositories */
controllers: [CarController, WheelController, OptionController],
providers: [CarService, WheelService, OptionService],
})
export class CarModule {}
... so by explicitely naming "Repository" for my entity.
While this does not work :
@Module({
imports: [TypeOrmModule.forFeature([Car, Wheel, Option])], /* define repositories */
controllers: [CarController, WheelController, OptionController],
providers: [CarService, WheelService, OptionService],
})
export class CarModule {}
Complete output :
[Nest] 18832 - 2022-06-22 16:34:13 ERROR [ExceptionHandler] Nest can't resolve dependencies of the OptionService (?). Please make sure that the argument OptionRepositoryRepository at index [0] is available in the CarModule context.
Potential solutions:
- If OptionRepositoryRepository is a provider, is it part of the current CarModule?
- If OptionRepositoryRepository is exported from a separate @Module, is that module imported within CarModule?
@Module({
imports: [ /* the Module containing OptionRepositoryRepository */ ]
})
Error: Nest can't resolve dependencies of the OptionService (?). Please make sure that the argument OptionRepositoryRepository at index [0] is available in the CarModule context.
Potential solutions:
- If OptionRepositoryRepository is a provider, is it part of the current CarModule?
- If OptionRepositoryRepository is exported from a separate @Module, is that module imported within CarModule?
@Module({
imports: [ /* the Module containing OptionRepositoryRepository */ ]
})
at Injector.lookupComponentInParentModules (/Users/me/Documents/project_test/node_modules/@nestjs/core/injector/injector.js:231:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `nest start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
I have the same error when I try to change "Wheel" by "WheelRepository" in my list of imports.
I can not understand in which case I should be able to use one or another method, and why in this case I have to mix both.
[ EDIT ]
I found why by comparing files : in my OptionService constructor, I was injecting @InjectRepository(OptionRepository) instead of @InjectRepository(Option).
I am still curious of knowing why this error happens because I do not understand the link between car module, which is like "above", and the injection in the service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,从 a>和 @nestjs/typeorm
typeormmodule.forfeature([... entities])imports typeormmodule提供程序来自TypeMommodule.forroot(),并使用其提供商令牌字符串在服务中提供。
和@InjectRepository(照片)给提供者令牌字符串,并像任何可注射的提供商一样使用它。
当您将@InjectRepository(optionrepository)放在构造函数中。 @nestjs/typeorm无法解决其提供商令牌字符串,因为optionrepository不是您给出的东西
From what I understand from https://docs.nestjs.com/recipes/sql-typeorm and the source of @nestjs/typeorm
TypeOrmModule.forFeature([...Entities]) imports TypeormModule providers coming from TypeormModule.forRoot() and make it available inside service using its provider token string.
and @InjectRepository(Photo) gives the provider token string and use it like any injectable provider.
when you put @InjectRepository(OptionRepository) in the constructor. @nestjs/typeorm couldn't resolve its provider token string because OptionRepository wasn't the thing you give to