我有两个模块:AuthModule
和UserModule
;
UserModule
的外观如下:
@Module({
imports: [
forwardRef(() => AuthModule),
TypeOrmModule.forFeature([
UserRepository,
UserAuthRepository,
UserSocialRepository,
])
],
controllers: [UserController],
exports: [UserAuthService, UserSocialService, UserService],
providers: [UserAuthService, UserSocialService, UserService],
})
export class UserModule {}
该模块具有3个服务:UserAuthService
,UserSocialService
和UserService
。
UserAuthService
的内容:
@Injectable()
export class UserAuthService {
/**
* UserAuthService constructor
* @param { UserAuthRepository } _userAuthRepository
* @param { UserRepository } _userRepository
* @param { UserService } _userService
* @param { UserSocialService } _userSocialService
*/
constructor(
private readonly _userAuthRepository: UserAuthRepository,
private readonly _userRepository: UserRepository,
private readonly _userService: UserService,
private readonly _userSocialService: UserSocialService
) {}
}
UserSocialService
的内容:
@Injectable()
export class UserSocialService {
/**
* UserSocialService constructor
* @param { UserSocialRepository } _userSocialRepository
*/
constructor(
private readonly _userSocialRepository: UserSocialRepository,
) {}
}
UserService
的内容:
@Injectable()
export class UserService {
/**
* UserService constructor
* @param { UserRepository } _userRepository
* @param { UserAuthService } _userAuthService
* @param { UserSocialService } _userSocialService
*/
constructor(
private readonly _userRepository: UserRepository,
private readonly _userAuthService: UserAuthService,
private readonly _userSocialService: UserSocialService
) {}
}
AuthModule
和UserModule
都具有forwardRef(() => Module)
的含义,因为它们彼此依赖。
但是,问题出在UserModule
内部,更具体地说是UserService
,由于某种原因,它无法解决UserAuthService
依赖性,从而产生了以下错误:
Error: Nest can't resolve dependencies of the UserService (UserRepository, ?, UserSocialService). Please make sure that the argument dependency at index [1] is available in the UserModule context.
Potential solutions:
- If dependency is a provider, is it part of the current UserModule?
- If dependency is exported from a separate @Module, is that module imported within UserModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
这是UserModule
的结构:
user/
controllers/
dtos/
exceptions/
models/
repositories/
services/
- index.ts
- user.auth.service.ts
- user.service.ts
- user.social.service.ts
subscribers/
-index.ts
因此,即使所有3个服务都在一个模块中,并且将它们导出并标记为提供程序,NestJS出于某种原因仍无法解决它们。
答案 0 :(得分:1)
我不确定100%,但是我认为您需要使用@InjectRepository
注入存储库,就像在example中一样:
@Injectable()
export class UserAuthService {
constructor(
@InjectRepository(UserAuthRepository)
private readonly _userAuthRepository: UserAuthRepository,
@InjectRepository(UserRepository)
private readonly _userRepository: UserRepository,
private readonly _userService: UserService,
private readonly _userSocialService: UserSocialService
) {}
}
答案 1 :(得分:0)
您的UserService
和UserAuthService
之间具有循环依赖关系。这两个服务都应使用@Inject(forwardRef(() => OtherService))
以确保即使通过循环依赖也可以进行注入。
通常,您应该尽量避免这些类之间的相互依赖,这是否意味着要打破类的范围,让其仅访问所需的内容,或者很难将这些类合并在一起。
>@Injectable()
export class UserService {
constructor(
@InjectRepository(UserRepository)
private readonly _userRepo: UserRepository,
@Inject(forwardRef(() => UserAuthService))
private readonly _userAuthService: UserAuthService,
private readonly _userSocialService: UserSocialService
) {}
}
@Injectable()
export class UserAuthService {
constructor(
@InjectRepository(UserAuthRepository)
private readonly _userAuthRepository: UserAuthRepository,
@InjectRepository(UserRepoitory)
private readonly _userRepository: UserRepository,
@Inject(forwardref(() => UserSerivce))
private readonly _userService: UserService,
private readonly _userSocialService: UserSocialService
) {}
}