简体   繁体   English

NestJS 中的模块

[英]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.

相关问题 NestJS:动态模块的缺点? - NestJS: Drawbacks of dynamic modules? 测试中的 NestJS 全局模块 - NestJS Global Modules in tests NestJS - 如何提供动态模块的服务 - NestJS - how to provide services of dynamic modules nestjs 模块 - beetwean 提供程序和导入的区别是什么 - nestjs modules - whats the diffrence beetwean providers and imports 在 NestJS 中使用依赖注入导入 TypeScript 模块 - Importing TypeScript modules with dependency injection in NestJS 如何使用 NestJS 向特定模块添加路由前缀? - How to add a route prefix to specific modules using NestJS? 使用 NestJS + Typescript + Webpack + node_modules 的单个文件包 - Single file bundle with NestJS + Typescript + Webpack + node_modules 如果节点中的所有内容都是模块,那么 NestJs 中的模块有什么意义? - if everything in node is a module than what is the point of modules in NestJs? 如何使用捆绑中的 node_modules 依赖项正确构建用于生产的 NestJS 应用程序? - How to correctly build NestJS app for production with node_modules dependencies in bundle? 为什么我的应用程序在运行时出现此错误,“ERROR in./node_modules/@nestjs/common/cache/cache.providers.js”? - Why is my app getting this error when I run it, “ERROR in ./node_modules/@nestjs/common/cache/cache.providers.js”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM