[英]Why cant nestj resolve dependencies?
So i have a Nest application and have gotten stuck on what feels like a very basic issue.所以我有一个 Nest 应用程序,并且陷入了一个非常基本的问题。
I need to use the PlaceVerificationRequestService in the PlaceService but cant inject it properly without receiving the following error:我需要在PlaceService中使用PlaceVerificationRequestService但无法正确注入它而不会收到以下错误:
Nest can't resolve dependencies of the PlaceService (PlaceRepository, ClientService, ?).
Nest 无法解析 PlaceService 的依赖项(PlaceRepository、ClientService、?)。 Please make sure that the argument dependency at index [2] is available in the PlaceModule context.
请确保索引 [2] 处的参数依赖项在 PlaceModule 上下文中可用。
My approach has been to just follow the same style as i did when importing & injecting the ClientService in to PlaceService but still it does not work and i cant figure out why.我的方法是遵循与将ClientService导入并注入PlaceService时相同的风格,但它仍然不起作用,我不知道为什么。
This is my code.这是我的代码。
place.module.ts place.module.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [Place, Client, PlaceVerificationRequest],
}),
],
providers: [PlaceService, ClientService, PlaceVerificationRequestService],
controllers: [PlaceController],
exports: [PlaceService],
})
place-verification-request.module.ts放置验证请求.module.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [PlaceVerificationRequest, Client, Place],
}),
],
providers: [PlaceVerificationRequestService, ClientService, PlaceService],
controllers: [PlaceVerificationRequestController],
exports: [PlaceVerificationRequestService],
})
place.service.ts地点.service.ts
@Injectable()
export class PlaceService {
constructor(
@InjectRepository(Place)
private readonly placeRepo: EntityRepository<Place>,
private readonly clientService: ClientService,
private readonly reqService: PlaceVerificationRequestService,
) {}
It feels like im missing something that is right in front of my nose since it feels very basic but i cant seem to spot it.感觉就像我错过了一些就在我鼻子前面的东西,因为它感觉很基本,但我似乎无法发现它。 Anyone that has a clue?
任何有线索的人? thanks
谢谢
Okay after 5 hours, the trick is... to read the docs.好的 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.
(gotta admit they could've come up with a clearer error message) (必须承认他们本可以提出更清晰的错误信息)
So basically the problem was that PlaceService was getting injected in PlaceVerificationRequestService and PlaceVerificationRequestService was getting injected in PlaceService so the trick is to use forwardRef
:所以基本上问题是 PlaceService 被注入 PlaceVerificationRequestService 而 PlaceVerificationRequestService 被注入 PlaceService 所以诀窍是使用
forwardRef
:
PlaceVerificationRequestService PlaceVerificationRequestService
@Inject(forwardRef(() => PlaceService))
private readonly placeService: PlaceService,
PlaceService地方服务
@Inject(forwardRef(() => PlaceVerificationRequestService))
private readonly reqService: PlaceVerificationRequestService,
Nest cannot figure out how to create a copy of the PlaceService because you have not provided all the different dependencies of the PlaceService. Nest 无法弄清楚如何创建 PlaceService 的副本,因为您没有提供 PlaceService 的所有不同依赖项。
That's why it asks is the <unknown_token>
a provider?这就是为什么它询问
<unknown_token>
是提供者吗? In other words, does the PlaceService need a copy of this unknown_token?换句话说,PlaceService 是否需要这个 unknown_token 的副本? Now that does not make sense to me, so what it probably wants to ask you is does it need a copy of that Place Verification Request Service, but the way you wrote this is giving you a confusing prompt, you know we get out of it what we put into it, so the error is not confusing, we just gave it something that made it give us a confusing prompt.
现在这对我来说没有意义,所以它可能想问你的是它是否需要一个地方验证请求服务的副本,但是你写这个的方式给你一个令人困惑的提示,你知道我们可以摆脱它我们把什么放进去,所以这个错误不会让人困惑,我们只是给了它一些东西,让它给我们一个令人困惑的提示。
You can try to write a test for this by doing something like this for example:您可以尝试通过执行以下操作来为此编写测试,例如:
import { Test } from '@nestjs/testing';
import { PlaceService } from './place/service';
import { PlaceVerificationRequestService } from './place-verification-request.service';
it('can create an instance of place service', async () => {
// Create a fake copy of the place verification request service
const fakePlaceVerificationRequestService = {
find: () => Promise.resolve([])
// add properties of the service as arguments
create: () => Promise.resolve()
};
const module = await Test.createTestingModule({
providers: [
PlaceService,
{
provide: PlaceVerificationRequestService,
useValue: fakePlaceVerificationRequestService
}
],
}).compile();
const service = module.get(PlaceService);
expect(service).toBeDefined();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.