[英]Nest can't resolve dependencies of the Service while compiling nest project
Getting below error while compiling the below code: I am new to nestjs and this is the attached sample application code.编译以下代码时出现以下错误:我是 nestjs 的新手,这是附加的示例应用程序代码。 I tried replacing UsersService in module.ts with UsersModule as well but it didn't work.我也尝试用 UsersModule 替换 module.ts 中的 UsersService 但它没有用。 What am i doing wrong ?我究竟做错了什么 ?
[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存储库: https : //github.com/richakhetan/task-manager-nest
You have a circular dependency between the AuthModule
and UserModule
and between the UserService
and AuthService
.你有之间的循环依赖AuthModule
和UserModule
和之间UserService
和AuthService
。 To resolve this, on both the modules and the services you need to use a forwardRef
.要解决此问题,您需要在模块和服务上使用forwardRef
。 Generally, this would just look like一般来说,这看起来像
@Module({
imports: [forwardRef(() => UserModule)],
providers: [AuthService],
exports: [AuthService],
})
export class AuthModule {}
@Module({
imports: [forwardRef(() => AuthModule)],
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
@Injectable()
export class AuthService {
constructor(@Inject(forwardRef(() => UserService)) private readonly userService: UserService) {}
}
@Injectable()
export class UserService {
cosntructor(@Inject(forwardRef(() => AuthService) private readonly authService: AuthService) {}
}
Forgot to add the exports
property忘记添加exports
属性
modules were not properly structured.模块结构不正确。 I removed the code causing circular dependency from the modules and created a new module creating a clear structure.我从模块中删除了导致循环依赖的代码,并创建了一个创建清晰结构的新模块。
Detailed Code can be found in the repository.详细代码可以在存储库中找到。 Repository : https://github.com/richakhetan/task-manager-nest存储库: https : //github.com/richakhetan/task-manager-nest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.