nestjs - Nest 无法解析 AuthService 的依赖关系

标签 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
第一个问题:
在 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 中 put 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?
可能的解决方案 现在我没有任何解决方案,我只是将其删除以了解问题所在
第三个问题:
删除 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.
有人解决了这个问题吗?

最佳答案

你有一个循环依赖,因为你的 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 无法决定先创建哪个,因此需要一个引用来传回,因此函数。我一直把容器的前向 ref 功能想象成说“嘿,保持一秒钟”(这是一张写着“UsersModule”的纸)然后转过身,在他的背转过身的情况下做一些挥动的 ARM ,然后转过身并用 UsersModule 替换这张纸!
您的第二个问题是您从未为 LocalStrategy 创建提供程序.它不存在于 AuthModule 中,因此无法导入 AuthService 或从 AuthModule 导出!

关于nestjs - Nest 无法解析 AuthService 的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63572923/

相关文章:

nestjs - 在 nestjs 中将 .html 文件作为字符串导入

NestJS 和 passport-local : AuthGuard ('local' ) validate() never get called

typescript - 找不到模块 'passport' 或其对应的类型声明

nestjs - UnauthorizedException 作为内部服务器错误交付

mongodb - NestJS + Mongoose + GraphQL : "populate" not working

node.js - 使用 NestJS 在 OPTIONS 上找不到 404

node.js - 与奇怪的语法错误作斗争 Unexpected token * error

nestjs - 将信息从 NestJS 中间件传递到 Controller /服务

typescript - NestJs JWT 身份验证返回 401