【问题标题】:Nest can't resolve dependencies of the JwtServiceNest 无法解析 JwtService 的依赖关系
【发布时间】:2021-11-17 18:51:25
【问题描述】:

我在这里创建了一些模块,但我遇到了一个错误

认证模块

   import { Module } from "@nestjs/common";
import { AuthService } from "./auth.service";
import { LocalStrategy } from "./local.strategy";
import { JwtStrategy } from "./jwt.strategy";
import { UsersModule } from "../users/users.module";
import { PassportModule } from "@nestjs/passport";
import { JwtModule, JwtService } from "@nestjs/jwt";
import { jwtConstants } from "./constants";
import { ConfigModule, ConfigService } from "@nestjs/config";

@Module({
    imports: [
        UsersModule,
        PassportModule,
        JwtModule.register({
            secret: jwtConstants.secret,
            signOptions: { expiresIn: "1d" },
        }),
    ],
    providers: [AuthService, LocalStrategy, JwtStrategy],
    exports: [AuthService, LocalStrategy, JwtStrategy],
})
export class AuthModule {}

电子邮件模块

  import { Module } from "@nestjs/common";
import EmailService from "./email.service";

import { ConfigModule } from "@nestjs/config";

import { EmailConfirmationService } from "./emailConfirmation.service";
import { EmailConfirmationController } from "./emailConfirmation.controller";
import { EmailConfirmationGuard } from "./guards/emailConfirmation.guard";
import { AuthModule } from "src/auth/auth.module";
import { UsersModule } from "src/users/users.module";

@Module({
    imports: [ConfigModule,AuthModule,UsersModule],
    providers: [EmailService,EmailConfirmationService,EmailConfirmationGuard],
    exports: [EmailConfirmationService,EmailConfirmationGuard],
    controllers : [EmailConfirmationController]
})
export class EmailModule {}

用户模块

import { Module } from "@nestjs/common";
import { UsersService } from "./users.service";
import { UsersController } from "./users.controller";
import { MongooseModule } from "@nestjs/mongoose";
import { UserSchema } from "./entities/user.entity";
import { EmailModule } from "src/email/email.module";

@Module({
    imports: [MongooseModule.forFeature([{ name: "User", schema: UserSchema }]),EmailModule],
    providers: [UsersService],
    exports: [UsersService],
    controllers: [UsersController],
})
export class UsersModule {}

我面临的错误

 [Nest] 9200  - 09/26/2021, 3:43:15 PM   ERROR [ExceptionHandler] Nest cannot create the EmailModule instance.
The module at index [1] of the EmailModule "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 [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> AuthModule -> UsersModule]
Error: Nest cannot create the EmailModule instance.
The module at index [1] of the EmailModule "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 [1] is of type "undefined". Check your import statements and the type of the module.

我错过了什么?

EmailConfirmationService is used in UsersController
UserService is used in EmailConfirmationService

【问题讨论】:

    标签: javascript jwt nestjs


    【解决方案1】:

    为了在您的 UserController 中使用 EmailService,并且在 EmailConfirmationService 中使用 UserService,您必须在 UserModule 中执行类似的操作:

    imports: [forwardRef(() => EmailModule)]
    

    在 EmailModule 内部为 UserModule 做同样的事情:

    imports: [forwardRef(() => UserModule)]
    

    其余的导入应该没有 forwardRef(()=>)

    你可以在这里阅读更多关于循环依赖的信息https://docs.nestjs.com/fundamentals/circular-dependency

    【讨论】:

      【解决方案2】:

      您的导入中列出了JwtService。导入仅适用于模块。

      也分享来自JwtService的代码,以便我们确保没有其他问题。

      更新: 如果EmailModule 想要使用来自AuthModule 的导出(在本例中为JwtService),则必须将AuthModule 添加到其导入数组中。

      这是 DI 系统的全部前提,模块之间通过将它们打算共享的东西放在exports 数组中来共享东西。之后,任何模块都可以将 Source 模块添加到其imports 数组中,以访问源导出的内容。错误消息为您逐字逐句说明:

      If JwtService is exported from a separate @Module, is that module imported within EmailModule? @Module({ imports: [ /* the Module containing JwtService */ ] })

      【讨论】:

        猜你喜欢
        • 2019-02-24
        • 2021-11-24
        • 1970-01-01
        • 2022-01-17
        • 2018-11-21
        • 2020-03-12
        • 2020-07-03
        • 2021-04-28
        • 2020-03-07
        相关资源
        最近更新 更多