所以我有一个Nest应用程序,遇到了一个非常基本的问题。
我需要使用PlaceService中的PlaceVerificationRequestService,但如果不收到以下错误,则无法正确注入:
Nest无法解析PlaceService的依赖项(PlaceRepository、ClientService,?(。请确保索引[2]处的参数依赖项在PlaceModule上下文中可用。
我的方法是遵循与导入&将ClientService注入PlaceService,但它仍然不起作用,我不知道为什么。
这是我的密码。
放置模块.ts
@Module({
imports: [
MikroOrmModule.forFeature({
entities: [Place, Client, PlaceVerificationRequest],
}),
],
providers: [PlaceService, ClientService, PlaceVerificationRequestService],
controllers: [PlaceController],
exports: [PlaceService],
})
放置验证请求.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,而PlaceVerificationRequest Service被注入到PlaceService,所以诀窍是使用forwardRef
:
PlaceVerificationRequestService
@Inject(forwardRef(() => PlaceService))
private readonly placeService: PlaceService,
PlaceService
@Inject(forwardRef(() => PlaceVerificationRequestService))
private readonly reqService: PlaceVerificationRequestService,
Nest无法找到如何创建PlaceService的副本,因为您没有提供PlaceService的所有不同依赖项。
这就是为什么它问<unknown_token>
是提供者吗?换句话说,PlaceService是否需要此unknown_token的副本?现在,这对我来说没有意义,所以它可能想问你的是,它需要一份位置验证请求服务的副本吗?但你写这封信的方式给了你一个令人困惑的提示,你知道,我们从中得到了我们投入的东西,所以这个错误并不令人困惑,我们只是给了它一些东西,让它给了我们一个令人混淆的提示。
你可以试着通过做这样的事情来写一个测试,例如:
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();
});