为什么nestjs不能解析依赖?
Why cant nestj resolve dependencies?
所以我有一个 Nest 应用程序,但被困在一个非常基本的问题上。
我需要在PlaceService[中使用PlaceVerificationRequestService 但无法在不收到以下错误的情况下正确注入它:
Nest can't resolve dependencies of the PlaceService (PlaceRepository, ClientService, ?). Please make sure that the argument dependency at index [2] is available in the PlaceModule context.
我的方法是遵循与我在导入和注入 ClientService 到 [=30= 时相同的风格]PlaceService 但它仍然不起作用,我不知道为什么。
这是我的代码。
place.module.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [Place, Client, PlaceVerificationRequest],
}),
],
providers: [PlaceService, ClientService, PlaceVerificationRequestService],
controllers: [PlaceController],
exports: [PlaceService],
})
place-验证-request.module.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [PlaceVerificationRequest, Client, Place],
}),
],
providers: [PlaceVerificationRequestService, ClientService, PlaceService],
controllers: [PlaceVerificationRequestController],
exports: [PlaceVerificationRequestService],
})
place.service.ts
@Injectable()
export class PlaceService {
constructor(
@InjectRepository(Place)
private readonly placeRepo: EntityRepository<Place>,
private readonly clientService: ClientService,
private readonly reqService: PlaceVerificationRequestService,
) {}
感觉就像我错过了就在我鼻子前面的东西,因为它感觉很基本,但我似乎无法发现它。有人知道吗?谢谢
5 小时后,诀窍是……阅读文档。
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> context.
Potential solutions:
- If <unknown_token> is a provider, is it part of the current <module>?
- If <unknown_token> is exported from a separate @Module, is that module imported within <module>?
@Module({
imports: [ /* the Module containing <unknown_token> */ ]
})
If the unknown_token above is the string dependency, you might have a circular file import.
(必须承认他们可以提出更清晰的错误消息)
所以基本上问题是 PlaceService 被注入到 PlaceVerificationRequestService 中,而 PlaceVerificationRequestService 被注入到 PlaceService 中,所以诀窍是使用 forwardRef
:
PlaceVerificationRequestService
@Inject(forwardRef(() => PlaceService))
private readonly placeService: PlaceService,
地点服务
@Inject(forwardRef(() => PlaceVerificationRequestService))
private readonly reqService: PlaceVerificationRequestService,
所以我有一个 Nest 应用程序,但被困在一个非常基本的问题上。
我需要在PlaceService[中使用PlaceVerificationRequestService 但无法在不收到以下错误的情况下正确注入它:
Nest can't resolve dependencies of the PlaceService (PlaceRepository, ClientService, ?). Please make sure that the argument dependency at index [2] is available in the PlaceModule context.
我的方法是遵循与我在导入和注入 ClientService 到 [=30= 时相同的风格]PlaceService 但它仍然不起作用,我不知道为什么。
这是我的代码。
place.module.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [Place, Client, PlaceVerificationRequest],
}),
],
providers: [PlaceService, ClientService, PlaceVerificationRequestService],
controllers: [PlaceController],
exports: [PlaceService],
})
place-验证-request.module.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [PlaceVerificationRequest, Client, Place],
}),
],
providers: [PlaceVerificationRequestService, ClientService, PlaceService],
controllers: [PlaceVerificationRequestController],
exports: [PlaceVerificationRequestService],
})
place.service.ts
@Injectable()
export class PlaceService {
constructor(
@InjectRepository(Place)
private readonly placeRepo: EntityRepository<Place>,
private readonly clientService: ClientService,
private readonly reqService: PlaceVerificationRequestService,
) {}
感觉就像我错过了就在我鼻子前面的东西,因为它感觉很基本,但我似乎无法发现它。有人知道吗?谢谢
5 小时后,诀窍是……阅读文档。
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> context.
Potential solutions:
- If <unknown_token> is a provider, is it part of the current <module>?
- If <unknown_token> is exported from a separate @Module, is that module imported within <module>?
@Module({
imports: [ /* the Module containing <unknown_token> */ ]
})
If the unknown_token above is the string dependency, you might have a circular file import.
(必须承认他们可以提出更清晰的错误消息)
所以基本上问题是 PlaceService 被注入到 PlaceVerificationRequestService 中,而 PlaceVerificationRequestService 被注入到 PlaceService 中,所以诀窍是使用 forwardRef
:
PlaceVerificationRequestService
@Inject(forwardRef(() => PlaceService))
private readonly placeService: PlaceService,
地点服务
@Inject(forwardRef(() => PlaceVerificationRequestService))
private readonly reqService: PlaceVerificationRequestService,