TypeOrmModule.forFeature()在TS中的参数应该是什么?



我正试图将存储库添加到我的TypeOrmModule.forFeature()中,这从我的理解中定义了已创建的存储库。

在文档后面,我看到我们可以简单地添加实体名称,如:imports: [TypeOrmModule.forFeature([User])],

在另一个教程中,我看到有人直接将存储库名称放入forFeature中,例如:

imports: [TypeOrmModule.forFeature([UserRepository])],

在我当前的代码中,我声明了几个实体和存储库。使用DataSource创建存储库,例如:

export 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 {}

…因此,通过显式地命名"repository"。为我的实体。

虽然这不起作用:

@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! project_test@0.0.1 start: `nest start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the project_test@0.0.1 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:

我有同样的错误,当我试图改变"车轮"通过"WheelRepository"在我的导入列表中

我不明白在哪种情况下我应该能够使用一种或另一种方法,以及为什么在这种情况下我必须混合使用这两种方法。

[EDIT]

我发现为什么通过比较文件:在我的OptionService构造函数中,我注入@InjectRepository(OptionRepository)而不是@InjectRepository(Option)。我仍然很想知道为什么会出现这个错误,因为我不理解car模块之间的链接,就像上面的"一样,和服务中的注入。

从我从https://docs.nestjs.com/recipes/sql-typeorm和@nestjs/typeorm的来源了解

TypeormModule. forfeature([…Entities])从TypeormModule. forroot()中导入TypeormModule提供程序,并使用其提供程序令牌字符串使其在服务中可用。

import { DataSource } from 'typeorm';
import { Photo } from './photo.entity';
export const photoProviders = [
{
provide: 'PHOTO_REPOSITORY', // provider token string
useFactory: (dataSource: DataSource) => dataSource.getRepository(Photo),
inject: ['DATA_SOURCE'],
},
];

和@InjectRepository(照片)给出了提供程序令牌字符串,并像使用任何可注入的提供程序一样使用它。

export class PhotoService {
constructor(
@Inject('PHOTO_REPOSITORY') //@InjectRepository(Photo)
private photoRepository: Repository<Photo>,
) {}
}

当你把@InjectRepository(OptionRepository)放在构造函数中时。@nestjs/typeorm无法解析它的提供商令牌字符串,因为OptionRepository不是你给

的东西
imports: [TypeOrmModule.forFeature([Car, Wheel, Option])]

最新更新