首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译Nest项目时nest无法解析服务的依赖关系

编译Nest项目时nest无法解析服务的依赖关系
EN

Stack Overflow用户
提问于 2020-10-11 16:17:10
回答 2查看 2.1K关注 0票数 1

编译以下代码时出错:我是nestjs新手,这是附带的示例应用程序代码。我也尝试用UsersService替换module.ts中的UsersModule,但它没有起作用。我做错什么了?

代码语言:javascript
复制
 [Nest] 19960 - 10/19/2020, 11:42:09 PM [NestFactory] Starting Nest application...
[Nest] 19960 - 10/19/2020, 11:42:09 PM [InstanceLoader] MongooseModule dependencies initialized +25ms
[Nest] 19960 - 10/19/2020, 11:42:09 PM [InstanceLoader] PassportModule dependencies initialized +1ms
[Nest] 19960 - 10/19/2020, 11:42:09 PM [ExceptionHandler] Nest can't resolve dependencies of the AuthService (?, JwtService). Please make sure that the argument UsersService at index [0] is available in the AuthService context.

Potential solutions:

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

Repoistory:https://github.com/richakhetan/task-manager-nest

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-28 18:36:41

模块的结构不适当。我从模块中删除了导致循环依赖的代码,并创建了一个新模块,创建了一个清晰的结构。

详细代码可以在存储库中找到。存储库:https://github.com/richakhetan/task-manager-nest

票数 0
EN

Stack Overflow用户

发布于 2020-10-11 16:28:59

AuthModuleUserModule 之间有循环依赖关系,UserServiceAuthService之间有循环依赖关系。要解决这个问题,您需要在模块和服务上使用forwardRef。一般情况下,这看起来就像

代码语言:javascript
复制
@Module({
  imports: [forwardRef(() => UserModule)],
  providers: [AuthService],
  exports: [AuthService],
})
export class AuthModule {}
代码语言:javascript
复制
@Module({
  imports: [forwardRef(() => AuthModule)],
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}
代码语言:javascript
复制
@Injectable()
export class AuthService {
  constructor(@Inject(forwardRef(() => UserService)) private readonly userService: UserService) {}
}
代码语言:javascript
复制
@Injectable()
export class UserService {
  cosntructor(@Inject(forwardRef(() => AuthService) private readonly authService: AuthService) {}
}

编辑

忘记添加exports属性

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64306303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档