NestJS can't resolve dependencies of the JWT_MODULE_OPTIONS

徘徊边缘 提交于 2019-12-11 14:31:28

问题


I'm failed to compile with this error:

Nest can't resolve dependencies of the JWT_MODULE_OPTIONS (?). Please make sure that the argument at index [0] is available in the JwtModule context. +52ms

I saw similar dependencies problems with modules & services, but they didn't work for me. Using JwtModule in my auth.module.ts:

import { JwtModule } from '@nestjs/jwt';
@Module({
    imports: [
        TypeOrmModule.forFeature([User, Role]),
        ConfigModule,
        PassportModule.register({ defaultStrategy: 'jwt' }),
        JwtModule.registerAsync({
            inject: [ConfigService],
            useFactory: async (configService: ConfigService) => ({
                secretOrPrivateKey: config.jwtSecret,
                type: configService.dbType as any,
                host: configService.dbHost,
                port: configService.dbPort,
                username: configService.dbUsername,
                password: configService.dbPassword,
                database: configService.dbName,
                entities: ['./src/data/entities/*.ts'],
                signOptions: {
                    expiresIn: config.expiresIn,
                },
            }),
        }),

    ],
    providers: [AuthService, JwtStrategy],
    controllers: [AuthController],
})
export class AuthModule { }

I have no idea how to fix this bug... Using jwt 6.1.1

Edit: In my previous project use jwt 6.0.0, so I downgrade it, but problem not fix.


回答1:


First, you are mixing the TypeORMModule configuration with the JWTModule configuration.

According to @nestjs/jwt source code (and docs), secretOrPrivateKey and signOptions. All the other parameters seem to be part of the TypeORMModule configuration.

Second, the ConfigService (which is dependency [0] of the JWT module) does not seem to exist anywhere in your code. So you are missing an import to a module where the ConfigService exists inside.

This is why the dependency load is failing (and that is what the error is throwing means)

Note that in your code you are missing an import of a module (ConfigModule in the following sample), which is the module that holds the ConfigService. Otherwise there is no way to inject this ConfigService from nowhere!

JwtModule.registerAsync({
  imports: [ConfigModule], // Missing this
  useFactory: async (configService: ConfigService) => ({
    signOptions: {
       expiresIn: config.expiresIn,
    },
    secretOrPrivateKey: config.jwtSecret,
  }),
  inject: [ConfigService], 
}),



回答2:


I somehow got it to work by adding

JwtModule.registerAsync({
  imports: [ConfigModule], // Missing this
  useFactory: async (configService: ConfigService) => ({
    signOptions: {
       expiresIn: config.expiresIn,
    },
    secretOrPrivateKey: config.jwtSecret,
  }),
  inject: [ConfigService], 
}),

in the app.module.ts and auth.module.ts



来源:https://stackoverflow.com/questions/57463523/nestjs-cant-resolve-dependencies-of-the-jwt-module-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!