【问题标题】:Nest can't resolve dependencies of AuthServiceNest 无法解析 AuthService 的依赖关系
【发布时间】:2020-12-13 19:05:15
【问题描述】:

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

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

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

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

错误:

[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

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

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

[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?

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

第三个问题: 移除 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.

有人解决了这个问题吗?

【问题讨论】:

标签: nestjs nestjs-passport nestjs-jwt


【解决方案1】:

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

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

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

// And the same for your UsersModule

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

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

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

AppModule <= CatModule <= PawsModule

另一个例子是双重导入

            <= CatModule
AppModule                  <= PawsModule
            <= DogModule 

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

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

您的第二个问题是您从未为LocalStrategy 创建提供程序。它不存在于 AuthModule 中,因此无法导入 AuthService 也无法从 AuthModule 中导出!

【讨论】:

  • 我差点给你投反对票,请在仔细阅读我的问题后更新你的答案,并查看我共享的 github 存储库
  • 1) 你看到我已经在使用 orwardRef(() => UsersModule)
  • 2) 你看到 LocalStrategy 已经在 Authmodule 里面了吗?
  • 我在解释你的第一个问题......无论如何。你的问题是你没有导入 LocalStrategy。它不在 AuthModule 中。将LocalStrategy 添加到提供者。动臂固定
  • 好答案!只是一个错字,应该在示例 UsersModule 中删除 forwardRef: (() =&gt; AuthModule) 中的冒号。
【解决方案2】:

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

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

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

AuthService 也一样

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

在 AuthModule 和 UserModule 你仍然需要使用 forwardRef

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

关于LocalStrategy,放在模块的providers数组中

【讨论】:

  • 这对我有用,谢谢!应该被接受的答案
猜你喜欢
  • 2021-01-28
  • 2020-03-18
  • 2022-01-17
  • 2018-11-21
  • 2020-03-12
  • 2020-07-03
  • 2021-04-28
  • 2020-03-07
  • 2019-12-15
相关资源
最近更新 更多