Nest 无法解析 UserService (?, +) 的依赖项。请确保索引 [0] 处的参数在当前上下文中可用

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

有一些问题,di/circular-references我确定我做错了,我只是看不到它。

如有任何帮助,我们将不胜感激

user.module.ts

@Module({
    imports: [
        TypeOrmModule.forFeature([User]),
        forwardRef(() => AuthModule)
    ],
    providers: [ UserService, TokenService ],
    exports: [ UserService ]
})
export class UserModule {}

auth.module.ts

@Module({
    imports: [ forwardRef(() => UserModule) ],
    controllers: [ AuthController ],
    providers: [
        AuthService,
        UserService,
        TokenService
    ],
    exports: [ AuthService, TokenService ]
})
export class AuthModule {}

app.module.ts

@Module({
  imports: [
    TypeOrmModule.forRoot(),
    forwardRef(() => UserModule),
    forwardRef(() => AuthModule),
  ],
})
export class AppModule {}

我得到 [ExceptionHandler] 无法读取未定义的 属性 'module'。

最初是“Nest 无法解析 UserService 的依赖关系。然后我完全删除了 UserModule 并只使用了 AuthModule,一切正常,然后决定在今天重新添加 UserModule 并将代码从 AuthModule 移回进入 UserModule,然后发现 forwardRef(() => ) 现在我得到了 cannot read 属性 'model'.

提前致谢

您还需要通过 forwardRef 注入 UserService 的(循环)依赖项,参见 CommonService example:

constructor(@Inject(forwardRef(() => TokenService)) private readonly tokenService: TokenService) {}

此外,提供者只能在一个模块中声明。如果你想在另一个模块中使用它们,导出那些提供者,然后只导入模块:UserModule 导入 AuthModule 并且 not 提供 AuthService 再次。这样所有导出的提供程序都可以在 UserModule.

中使用

因此,从 AuthModule 的提供商列表中删除 UserService 并从 UserModule 中删除 TokenService

如果您在使用 TypeORM 将存储库添加到服务时出现上述错误,则在模块文件中添加以下代码

imports: [
  TypeOrmModule.forFeature([<your-entity>])
],

当我在数组的提供程序中有 2 个连续的逗号时出现错误

  providers: [
    AService,
    BService,, // <- NOT ALLOWED CIRCULAR PROBLEM 
  ],