我有一个 NestJS 问题,它似乎只适用于 1 个模块,其他所有模块都可以正常工作。我有以下模块。
错误是:
[ExceptionHandler] Nest can't resolve dependencies of the ApplicationService (ApplicationModel, AwsService, UserService, ?, JobService). Please make sure that the argument at index [3] is available in the ApplicationModule context.
该AgencyService是[3]。如果我AgencyModule从ApplicationModuleNestJS 中删除成功编译,我可以进行 API 调用。
AgencyModule,
ApplicationModule,
AuthModule,
JobModule,
UserModule,
所有这些模块都是他们的服务提供商的其他模块所需要的,所以不要使用forwardRef()我刚刚制作的方法在彼此之间导入它们Global()- 可能不是最佳实践,但嘿嘿(它有效)。
我的AppModule档案。
@Module({
imports: [
MongooseModule.forRootAsync({
useFactory: (configService: ConfigService) => ({
uri: configService.get('MONGO_DB_URL'),
useNewUrlParser: true,
}),
imports: [ConfigModule],
inject: [ConfigService],
}),
ConfigModule,
AgencyModule,
ApplicationModule,
AuthModule,
DevModule,
JobModule,
UserModule,
VideoModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
每个模块文件夹具有以下结构。
agency/
- dto/
- agency.controller.ts
- agency.interface.ts
- agency.schema.ts
- agency.service.ts
- agency.module.ts
我的AgencyModule档案。
@Global()
@Module({
imports: [
SharedModule,
MongooseModule.forFeature([{ name: 'Agency', schema: AgencySchema }]),
],
controllers: [
AgencyController,
],
providers: [
AgencyService,
AwsService,
],
exports: [
AgencyService,
],
})
export class AgencyModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
consumer
.apply()
.forRoutes(
{ path: 'agency', method: RequestMethod.GET },
);
}
}
我的AgencyService档案。
@Injectable()
export class AgencyService {
constructor(
@InjectModel('Agency') private readonly agencyModel: Model<Agency>,
private readonly awsService: AwsService,
private readonly applicationService: ApplicationService,
) {
//
}
// More stuff here but not worth adding to the snippet.
}
这AwsService是一个没有模块的共享服务。
喵喔喔
相关分类