Nest无法解析导入JwtService的服务的依赖项 [英] Nest can't resolve dependencies of the service which imports JwtService
问题描述
我正在尝试使用 @ nestjs/jwt .特别是它的registerAsync
方法(我的配置服务异步加载配置).我正在AuthModule
中注册JwtModule
,然后为每个登录/注册提供程序加载特定的模块.然后,将JwtService
添加到EmailService
的提供程序中,但是失败.
I am trying to use @nestjs/jwt. Particularly its registerAsync
method (my config service loads the configuration asynchronously). I am registering JwtModule
in the AuthModule
, which then loads specific modules for each login/registration providers. Then I add JwtService
to the providers of EmailService
but it fails.
应用程序的结构如下(非常示意):
The structure of the application is as follows (very schematic):
app.module.ts
@Module({
imports: [
AuthModule,
...
]
})
export class ApplicationModule {}
auth.module.ts
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.registerAsync({
useFactory: async (config: ConfigService) => ({
secretOrPrivateKey: config.get('jwt.secret')
}),
inject: [ConfigService]
}),
EmailAuthModule
],
exports: [JwtModule]
})
export class AuthModule {}
email.module.ts
@Module({
imports: [...],
controllers: [...],
providers: [EmailService, ...]
})
export class EmailAuthModule {}
email.service.ts
@Injectable()
export class EmailService {
constructor(
private readonly jwtService: JwtService
) {}
}
应用程序启动时失败,并显示以下错误:
Application fails with this error upon startup:
Nest can't resolve dependencies of the EmailService (UsersService, ?). Please make sure that the argument at index [1] is available in the current context. +70ms
Error: Nest can't resolve dependencies of the EmailService (UsersService, ?). Please make sure that the argument at index [1] is available in the current context.
at Injector.lookupComponentInExports (/Users/.../api/node_modules/@nestjs/core/injector/injector.js:146:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at Object.<anonymous> (/Users/.../api/node_modules/ts-node/src/_bin.ts:177:12)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
我想念什么?
推荐答案
服务不是全局的,只能在自身提供的模块中使用或从导出服务的另一个模块中导入.
Services are not global but can only be used in the modules that provide them themselves or import them from another module that exports the service.
这里的问题是EmailService
依赖于JwtService
,但是EmailAuthModule
既不提供JwtService
本身,也没有导入导出JwtService
的模块. (不幸的是,您在这里省略了EmailAuthModule
的imports
.)
The problem here is that the EmailService
depends on the JwtService
but the EmailAuthModule
does neither provide the JwtService
itself nor import a module that exports the JwtService
. (Unfortunately, you left out the EmailAuthModule
's imports
here.)
因此,要解决此问题,您必须导入JwtModule
本身或另一个将JwtModule
导出到EmailAuthModule
中的模块.
So to solve this you have to import either the JwtModule
itself or another module that exports the JwtModule
in the EmailAuthModule
.
这篇关于Nest无法解析导入JwtService的服务的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!