繁体   English   中英

NestJS 中的模块

[英]Modules in NestJS

有人可以告诉我为什么我有这个错误:

[Nest] 556   - 2020-06-10 18:52:55   [ExceptionHandler] Nest can't resolve dependencies of the JwtService (?). Please make sure that the argument JWT_MODULE_OPTIONS at index [0] is available in the JwtModule context.

Potential solutions:
- If JWT_MODULE_OPTIONS is a provider, is it part of the current JwtModule?
- If JWT_MODULE_OPTIONS is exported from a separate @Module, is that module imported within JwtModule?
  @Module({
    imports: [ /* the Module containing JWT_MODULE_OPTIONS */ ]
  })

我的模块:

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt'}),
    JwtModule.register({
      secret: 'topSecret51',
      signOptions: {
        expiresIn: 3600
      },
    }),
    TypeOrmModule.forFeature([User])
  ],
  controllers: [AuthController],
  providers: [AuthService, UserService]
})
export class AuthModule {}
@Module({
  controllers: [UserController],
  providers: [UserService, AuthService],
  imports: [AuthModule]
})
export class UserModule {}
@Module({
  imports: [
    TypeOrmModule.forRoot(typeOrmConfig),
    UserModule,
    AuthModule
  ],
})
export class AppModule {}

我尝试改变所有这些但在所有这些中我的应用程序都不起作用

谢谢你的帮助

///////////////////////////////////////// //

如果您在 JwtService 中使用 controller 的某些依赖项,则需要在当前 AuthModule 中添加 JWT_MODULE_OPTIONS 作为提供程序。 查看nestjs 文档 他们已经解释了如何注册自定义提供程序。

当我没有正确导入/初始化JwtModule时,我收到了这个错误。 JwtModule应该处理这个错误来自的JwtService初始化。 我会从那里开始。 如果您在测试环境中使用AuthModule ,而扩展需要JwtModule ,那么您需要手动定义JwtModule类似的东西:

    beforeAll(async () => {
        const testingModule: TestingModule = await Test.createTestingModule({
            imports: [
                ConfigModule.forRoot({ isGlobal: true }),
                MongooseModule.forFeature([{ name: 'User', schema: userSchema }]),
                JwtModule.registerAsync({
                    imports: [ConfigModule],
                    inject: [ConfigService],
                    useFactory: async (configService: ConfigService) => ({
                        secret: configService.get('JWT_SECRET'),
                        signOptions: { expiresIn: '1d' }
                    })
                }),
            ],

暂无
暂无

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

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