Nest 无法解析 AuthService 的依赖关系

San*_*ano 2 nestjs nestjs-passport nestjs-jwt

我正在关注此处提供的文档

https://docs.nestjs.com/techniques/authentication#jwt-functionality

为了获得更快的支持,我创建了一个有问题的 git 存储库

https://github.com/Sano123456/nestjs-jwt

node -v -> v10.15.2
npm -v -> 6.14.6
nest -v -> 7.4.1
Run Code Online (Sandbox Code Playgroud)

第一个问题: 在 AuthModule 中,如果我按照文档中的说明进行操作并只导入 UserModule,它会返回 UserModule 和 AuthModule 之间循环依赖的错误

@Module({
  imports:[
    UsersModule,
    PassportModule
  ],
  providers: [AuthService],
  controllers: [AuthController],
  exports:[AuthService, LocalStrategy]
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)

错误:

[ExceptionHandler] Nest cannot create the AuthModule instance.
The module at index [0] of the AuthModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [0] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> UsersModule] +6ms
Run Code Online (Sandbox Code Playgroud)

在导入 AuthModule 数组而不是 UserModule 中的可能解决方案提出 forwardRef(() => UsersModule),这实际上消除了错误,但不确定这是否是正确的方法

第二个问题: 它说找不到 LocalStrategy 类,即使它存在并在 AuthModule 中声明

[ExceptionHandler] Nest cannot export a provider/module that is not a part of the currently processed module (AuthModule). Please verify whether the exported LocalStrategy is available in this particular context.Is LocalStrategy part of the relevant providers/imports within AuthModule?
Run Code Online (Sandbox Code Playgroud)

现在可能的解决方案我没有任何解决方案,我只是将其删除以了解问题所在

第三个问题:删除LocalStrategy后,

[ExceptionHandler] Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.

Potential solutions:
- If dependency is a provider, is it part of the current AuthModule?
- If dependency is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })
 +1ms
Error: Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.
Run Code Online (Sandbox Code Playgroud)

有人解决了这个问题吗?

San*_*ano 14

** 问题“无法解决依赖关系”的解决方案 **

对于第一和第二个问题,最终的解决方案是在 UserService 中使用 @Inject(forwardRef(() => AuthService)) 这里是示例

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User) private readonly UserRepository: Repository<User>,
        private readonly config: ConfigService,
        @Inject(forwardRef(() => AuthService)) //<--- 
        private readonly authService: AuthService,
    ) {
        
    }
Run Code Online (Sandbox Code Playgroud)

AuthService 中也有同样的事情

@Injectable()
export class AuthService {
  private client: any;
  constructor(
    @Inject(forwardRef(() => UserService))//<--- here
    private readonly userService: UserService,
    @InjectConfig() private readonly config,
  ) {
  }
Run Code Online (Sandbox Code Playgroud)

在 AuthModule 和 UserModule 中,您仍然需要使用forwardRef

我以前从未使用过这个解决方案,也从来不需要它,但这解决了我的问题

关于LocalStrategy,将其放入模块的providers数组中


sou*_*rri 7

你有一个循环依赖,因为你的 UsersModule 和你的 AuthModule 相互导入。因此,您有 2 个选项可以修复此问题。

第一个是进行前向引用,这将允许同时构建两个模块,一旦构建,传入所需的引用。这是这样做的:

@Module({
  imports: [
    forwardRef(() => UsersModule),
  ],
  ...
})
export class AuthModule {}

// And the same for your UsersModule

@Module({
  imports: [
    forwardRef(() => AuthModule),
  ],
})
export class UsersModule {}
Run Code Online (Sandbox Code Playgroud)

第二种选择是消除彼此的依赖关系。这并不总是可能的,通过模块的名称猜测我认为这是不可能的。您不想让 auth 模块访问用户模块之外的用户模块的数据库和服务等。但我会给你一些关于模块继承的建议。

nestjs 中的模块是异步构建的。这意味着它们的结构必须像一棵级联树,其中一个模块导入所有其他模块。线性的东西看起来像这样

AppModule <= CatModule <= PawsModule
Run Code Online (Sandbox Code Playgroud)

另一个例子是双重导入

            <= CatModule
AppModule                  <= PawsModule
            <= DogModule 
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,paws 模块被创建一次,并导入到 cat 和 dog 模块中。这意味着 paws 模块将在 cat 和 dog 模块之前首先构建。

因此,要解释您的错误,您需要从线性代码的角度思考,以及模块导入树将如何共享相互导入的模块。因为它们相互导入,所以 nest 无法决定先创建哪个,因此需要一个引用来传回,因此函数。我一直把容器的前向 ref 功能想象成说“嘿,保持一秒钟”(这是一张写着“UsersModule”的纸)然后转过身来,在他转过身的情况下做一些波浪式的手臂,然后转过身来并用 UsersModule 替换这张纸!

您的第二个问题是您从未为LocalStrategy. 它不存在于 AuthModule 中,因此无法导入 AuthService 或从 AuthModule 导出!


Rob*_*ell 5

对我来说,像 Sano 的回答那样仅在两个服务上使用forwardRef 是不够的;我还必须在两个循环依赖模块上转发引用。

在这两项服务中:

https://docs.nestjs.com/fundamentals/circular-dependency#forward-reference

import { forwardRef, Inject } from '@nestjs/common';
...
@Inject(forwardRef(() => UserService))`
Run Code Online (Sandbox Code Playgroud)

在两个模块中:

https://docs.nestjs.com/fundamentals/circular-dependency#module-forward-reference

import { forwardRef } from '@nestjs/common';
...
imports: [
   forwardRef(() => UsersModule),
],
Run Code Online (Sandbox Code Playgroud)