我在这里创建了一些模块,但我遇到了一个错误
Auth模块
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发布于 2021-09-29 14:18:22
为了在UserController中使用EmailService,并在EmailConfirmationService中使用UserService,您必须在UserModule中执行以下操作:
imports: [forwardRef(() => EmailModule)]在EmailModule内部,对UserModule也做同样的事情:
imports: [forwardRef(() => UserModule)]其余的导入应该没有forwardRef(()=>)
您可以在这里阅读有关循环依赖的更多信息https://docs.nestjs.com/fundamentals/circular-dependency
发布于 2021-09-24 13:45:34
您的导入中列出了JwtService。导入仅适用于模块。
也分享来自JwtService的代码,这样我们就可以确保没有其他问题。
更新:如果EmailModule希望使用来自AuthModule的导出(在本例中为JwtService),它必须将AuthModule添加到其导入数组中。
这是DI系统的整个前提,模块通过将它们想要共享的东西放在exports数组中来实现彼此之间的共享。在此之后,任何模块都可以将源模块添加到其imports数组中,以访问源导出的内容。错误消息从字面上为您解释了它:
If JwtService is exported from a separate @Module, is that module imported within EmailModule? @Module({ imports: [ /* the Module containing JwtService */ ] })
https://stackoverflow.com/questions/69315819
复制相似问题