[英]Modules in NestJS
Can someone tell me why i have this error:有人可以告诉我为什么我有这个错误:
[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 */ ]
})
my modules:我的模块:
@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 {}
i try to change in all of them but in all of them my app does not work我尝试改变所有这些但在所有这些中我的应用程序都不起作用
thanks for any help谢谢你的帮助
//////////////////////////////////////////////////// ///////////////////////////////////////// //
If you are using some dependencies of the controller in JwtService, you need to add JWT_MODULE_OPTIONS as provider inside the current AuthModule.如果您在 JwtService 中使用 controller 的某些依赖项,则需要在当前 AuthModule 中添加 JWT_MODULE_OPTIONS 作为提供程序。 Have a look at the nestjs documentation .查看nestjs 文档。 They have explained on how to register custom providers.他们已经解释了如何注册自定义提供程序。
I received this error when I did not correctly import / initialize the JwtModule
.当我没有正确导入/初始化JwtModule
时,我收到了这个错误。 The JwtModule
should handle the JwtService
initialization which this error comes from. JwtModule
应该处理这个错误来自的JwtService
初始化。 I would start there.我会从那里开始。 If you're using the AuthModule
in your test environment which by extension needs the JwtModule
then you'll need to manually define your JwtModule
with something similar:如果您在测试环境中使用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.