Nest 无法解析 ResolutionSevice 的依赖项

Nest can't resolve dependencies of the ResolutionSevice

我目前正在探索 nestjs 并遇到了这个错误:

Error: Nest can't resolve dependencies of the ResolutionService (?). Please make sure that the argument ResolutionRepository at index [0] is available in the ResolutionService context.

Potential solutions:

  • If ResolutionRepository is a provider, is it part of the current ResolutionService?
  • If ResolutionRepository is exported from a separate @Module, is that module imported within ResolutionService? @Module({ imports: [ /* the Module containing ResolutionRepository */ ] })

我做错了什么?

resolution.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ResolutionEntity } from './resolution.entity';
import { ResolutionService } from './resolution.service';
import { ResolutionRepository } from './resolution.repository';

@Module({
  imports: [TypeOrmModule.forFeature([ResolutionEntity])],
  providers: [ResolutionRepository, ResolutionService],
  exports: [ResolutionService],
})
export class ResolutionModule {}

resolution.service.ts

import { Injectable } from '@nestjs/common';
import { ResolutionEntity } from './resolution.entity';
import { ResolutionRepository } from './resolution.repository';

@Injectable()
export class ResolutionService {
  constructor(private readonly resolutionRepository: ResolutionRepository) {}

  async getAllByName(name: string): Promise<ResolutionEntity[]> {
    return this.resolutionRepository.getAllByName(name);
  }
}

您的应用程序中某处的 imports 数组中有 ResolutionService。提供商 从不 进入 imports 数组,仅进入 providers。如果您在另一个模块中需要此提供程序,ResolutionModule 应该在 providersexports 数组中包含 ResolutionService,然后这个新模块应该包含 ResolutionModuleimports 数组中。

而不是

TypeOrmModule.forFeature([ResolutionEntity])

也许你想要

TypeOrmModule.forFeature([ResolutionEntity, ResolutionRepository])