【问题标题】:Modules in NestJSNestJS 中的模块
【发布时间】:2020-09-30 04:32:44
【问题描述】:

谁能告诉我为什么会出现这个错误:

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

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

感谢您的帮助

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

【问题讨论】:

  • 你能展示UserServiceAuthService的构造函数吗? JwtService 在哪里使用?你commoncore的版本是一样的,@nestjs/jwt的包是最新的吗?

标签: javascript typescript module node-modules nestjs


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    当我没有正确导入/初始化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' }
                        })
                    }),
                ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 2019-08-06
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多