簡體   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