Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NestJS Nest can';t解析用户服务的依赖关系_Nestjs - Fatal编程技术网

NestJS Nest can';t解析用户服务的依赖关系

NestJS Nest can';t解析用户服务的依赖关系,nestjs,Nestjs,我正在尝试创建我的自定义JwtModule。我制作了JwtModule动态模块,以便使用dotenv提供私钥 jwt.module.ts: 从'@nestjs/common'导入{DynamicModule,Module}; 从“./jwt.constants”导入{CONFIG_OPTIONS}; 从“/jwt.interfaces”导入{JwtModuleOptions}; 从“/jwt.service”导入{JwtService}; @模块({}) 导出类JWT模块{ 静态forRoot(

我正在尝试创建我的自定义
JwtModule
。我制作了
JwtModule
动态模块,以便使用
dotenv
提供私钥

jwt.module.ts:

从'@nestjs/common'导入{DynamicModule,Module};
从“./jwt.constants”导入{CONFIG_OPTIONS};
从“/jwt.interfaces”导入{JwtModuleOptions};
从“/jwt.service”导入{JwtService};
@模块({})
导出类JWT模块{
静态forRoot(选项:JwtModuleOptions):动态模块{
返回{
模块:JWT模块,
供应商:[
{
提供:配置_选项,
useValue:选项,
},
JwtService,
],
导出:[JwtService],
};
}
}
jwt.service.ts:

从'@nestjs/common'导入{Inject,Injectable};
从“./jwt.constants”导入{CONFIG_OPTIONS};
从“/jwt.interfaces”导入{JwtModuleOptions};
@可注射()
导出类JWTS服务{
建造师(
@注入(配置选项)私有只读选项:JwtModuleOptions,
) {}
//我会在这里做点什么
}
users.module.ts:

从'@nestjs/common'导入{Module};
从'@nestjs/typeorm'导入{TypeOrmModule};
从'src/jwt/jwt.module'导入{JwtModule};
从“./entities/User.entity”导入{User};
从“./users.resolver”导入{UsersResolver};
从“/users.service”导入{UsersService};
@模块({
导入:[TypeOrmModule.forFeature([User]),JwtModule],
提供者:[UsersResolver,UsersService],
})
导出类UsersModule{}
users.service.ts:

从'@nestjs/common'导入{Injectable};
从'@nestjs/typeorm'导入{InjectRepository};
从'src/common/common.constants'导入{InternalServerErrorOutput};
从'src/jwt/jwt.service'导入{JwtService};
从“typeorm”导入{Repository};
进口{
CreateAccountInput,
CreateAccountOutput,
}来自“./dtos/create account.dto”;
从“./entities/User.entity”导入{User};
@可注射()
导出类用户服务{
建造师(
@注入存储库(用户)
私有只读用户存储库:存储库,
专用只读jwtService:jwtService,
) {}
//我会在这里做点什么
}
我将JwtModule导入到
app.module.ts
中,如下所示:

JwtModule.forRoot({privateKey:process.env.JWT_PRIVATE_KEY})
但是,当我尝试运行此应用程序时,控制台会打印一个错误:

Nest can't resolve dependencies of the UsersService (UserRepository, ?).
Please make sure that the argument JwtService at index [1] is available in the UsersModule context.

Potential solutions:
- If JwtService is a provider, is it part of the current UsersModule?
- If JwtService is exported from a separate @Module, is that module imported within UsersModule?
  @Module({
    imports: [ /* the Module containing JwtService */ ]
  })


由于我导出了
JwtService
并将
JwtModule
导入到
UsersModule
中,因此错误提示没有帮助。使用
@Global
JwtModule
是一个简单的解决方案,但由于我不会在其他地方使用JwtModule,因此我想知道不使其全球化的方法。任何帮助都将不胜感激。谢谢大家!

动态模块就是这样,动态的。当它们即将被使用时,需要调用它们的配置/注册方法。您在
AppModule
中注册了
JwtModule
,但没有在
UserModule
中注册,因此
UserModule
不会看到导出了
JwtService
的返回模块,它会看到没有提供程序和导出的模块。有两种方法可以解决这个问题

  • 您可以直接在
    UserModule
    中注册
    JwtModule
    ,如果不打算重复使用,这可能不是一个坏主意

  • 您可以创建一个包装器模块tat导入
    JwtModule.forRoot()
    并导出
    JwtModule
    ,然后在需要
    JwtModule
    的地方导入包装器模块,并每次都返回配置的模块

  • 这基本上是矫枉过正,但很酷,您可以遵循RxJS主题方法,这样您就可以执行类似于
    JwtMoule.externallyConfigured(0)
    的操作,并获得在
    AppModule
    中进行的配置。又一次,杀伤力过大,但很酷