简体   繁体   English

Nest 无法解析 JwtService 的依赖关系

[英]Nest can't resolve dependencies of the JwtService

I have created some modules here but i am facing an error我在这里创建了一些模块,但我遇到了一个错误

Auth module认证模块

   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 {}

Email module电子邮件模块

  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 {}

User module用户模块

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 {}

Error I am facing我面临的错误

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

What am i missing ?我错过了什么?

EmailConfirmationService is used in UsersController
UserService is used in EmailConfirmationService

You have JwtService listed in your imports.您的导入中列出了JwtService Imports are for modules only.导入仅适用于模块。

Share the code from JwtService as well so that we can make sure there are no other issues.也分享来自JwtService的代码,以便我们确保没有其他问题。

Update: If the EmailModule wants to use the exports from the AuthModule (JwtService in this case), it must add the AuthModule to its imports array.更新:如果EmailModule想要使用来自AuthModule的导出(在本例中为 JwtService),它必须将AuthModule添加到其导入数组中。

This is the entire premise of the DI system, modules share things between eachother by placing the thing they intend to share in the exports array.这是 DI 系统的全部前提,模块之间通过将它们打算共享的东西放在exports数组中来共享东西。 After that, any module can add the Source module to its imports array to gain access to the things that the source exported.之后,任何模块都可以将 Source 模块添加到其imports数组中,以访问源导出的内容。 The error message literally spells it out for you:错误消息从字面上为您说明了这一点:

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

In order to use EmailService in you UserController and also UserService is used in EmailConfirmationService then you have to do something like this in UserModule:为了在您的 UserController 中使用 EmailService,并且在 EmailConfirmationService 中使用 UserService,您必须在 UserModule 中执行以下操作:

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

and inside the EmailModule do the same thing for the UserModule:在 EmailModule 内部对 UserModule 做同样的事情:

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

Rest of the imports should be without the forwardRef(()=>)其余的导入应该没有 forwardRef(()=>)

you can read more about circular dependency here https://docs.nestjs.com/fundamentals/circular-dependency您可以在此处阅读有关循环依赖的更多信息https://docs.nestjs.com/fundamentals/circular-dependency

确保 tsconfig.json 中的目标是tsconfig.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM