首页 > 解决方案 > 使用装饰器时,Nestjs 无法解析依赖索引 [0]

问题描述

我是 NestJs 的新手,我有一个无法解决的问题。

UsersService 有两个依赖项

-TypeOrm UsersRepository 注入 -WalletsService 注入

Typeorm 注入由装饰器完成,如下所示。

//UsersService
@Injectable()
export class UsersService implements IUserService {
  constructor(
    @InjectRepository(Users)
    private usersRepository: Repository<Users>,
    private readonly walletsService: WalletsService,
  ) { }

每当我更改第一次注射时,它都无法解决。

可能我错过了一些东西。下面有所有的照片和描述

//UsersModule
@Module({
  controllers: [UsersController],
  exports: [UsersService],
  imports: [WalletsModule, TypeOrmModule.forFeature([Users])],
  providers: [UsersService],
})

export class UsersModule { }

//WalletsModule
@Module({
  controllers: [WalletsController],
  exports: [WalletsService],
  imports: [TypeOrmModule.forFeature([Wallets])],
  providers: [WalletsService]
})

export class WalletsModule { }

[当用户存储库首次注入时]

Nest can't resolve dependencies of the UsersService (?, WalletsService). Please make sure that the argument UsersRepository at index [0] is available in the UsersService context.

Potential solutions:
- If UsersRepository is a provider, is it part of the current UsersService?
- If UsersRepository is exported from a separate @Module, is that module imported within UsersService?
  @Module({
    imports: [ /* the Module containing UsersRepository */ ]
  })

[钱包服务首次注入时]

Nest can't resolve dependencies of the UsersService (?, UsersRepository). Please make sure that the argument WalletsService at index [0] is available in the UsersService context.

Potential solutions:
- If WalletsService is a provider, is it part of the current UsersService?
- If WalletsService is exported from a separate @Module, is that module imported within UsersService?
  @Module({
    imports: [ /* the Module containing WalletsService */ ]
  })

先感谢您。我希望它的描述性足够。有美好的一天!

标签: nestjstypeorm

解决方案


对于来自评论的答案的后代:

数组中有一项服务imports会导致 Nest 尝试解决问题时出现问题。一般来说,知道 Nest 的错误是以下形式的,就可以开始调试这些错误

Nest 无法解析 UsersService 的依赖项 (<dependencies_with_unknown_as_?>。请确保索引 [] 处的参数 <unknown_dependency> 在 <related_module> 上下文中可用。


推荐阅读