mongodb - Nest can't resolve dependencies of the service
问题描述
Is it possible to inject many MongoDB models into one resolver and then use them?
I was trying to do it like that: first I added SectionSchema and SectionsService import to PostsModule
@Module({
imports: [MongooseModule.forFeature([{name: 'Post', schema: PostSchema}, {name: 'Section', schema: SectionSchema}])],
providers: [PostsResolver, PostsService, SectionsService],
})
export class PostsModule {}
Then I add all Schema imports to SectionModule like that:
@Module({
imports: [MongooseModule.forFeature([
{name: 'Section', schema: SectionSchema},
{name: 'Post', schema: PostSchema},
{name: 'Project', schema: ProjectSchema},
{name: 'Tutorial', schema: TutorialSchema},
])],
providers: [SectionsResolver, SectionsService],
})
export class SectionsModule {}
And in the end I injected all this models inside constructor of my SectionsService
@Injectable()
export class SectionsService {
constructor(
@InjectModel('Section') private readonly sectionModel: Model<SectionEntity>,
@InjectModel('Post') private readonly postModel: Model<PostEntity>,
@InjectModel('Project') private readonly projectModel: Model<ProjectEntity>,
@InjectModel('Tutorial') private readonly tutorialModel: Model<TutorialEntity>) {}
// find, create methods ...
}
When i am trying to run my project with npm run start:dev
I am getting this error :
Nest can't resolve dependencies of the SectionsService (SectionModel, PostModel, ?, TutorialModel). Please make sure that the argument ProjectModel at index [2] is available in the PostsModule context.
Is it possible to inject many MongoDB models ?
ResolverProperty
@ResolveProperty(() => [SectionEntity])
async sections(@Parent() { _id }: PostEntity): Promise<SectionEntity[]> {
const posts: any = await this.postsService.findAll();
return this.sectionsService.findAll(_id, ArticleType.POST);
}
解决方案
Your section service is fine, you have your SectionsService
provided in your PostModule
which is where the error is coming from, as your PostModule
doesn't provide a context for the ProjectModel
the SectionService
depends on. If your PostService
needs your SectionService
to function properly, consider exporting the SectionsService
from your SectionsModule
and importing the SectionsModule
in your PostModule
.
You need to export the SectionsService
from the SectionsModule
so it can be used in other modules. In your SectionsModule
you should add this line exports: [SectionsService]
and in your PostModule
you should have imports: [SectionsModule, /*rest of your imports */]
so that the module has the context required.
By exporting SectionsService
we are being explicit in telling Nest "this service will be used outside of its original module". Nest is very oriented around the ideas of modularization and separation of concerns, meaning that a lot of things, when made in a certain way, can be plug n play, in theory. Every service we make in Nest, by default, is only scoped to the module in which it is in the providers array. To make it available in any other module we need to add it to the exports array, thus, clearing the scope and saying, so long as this module is imported, this service is available
推荐阅读
- python - 我的后缀数组生成代码的时间复杂度是多少
- r - R数据框如何仅使用列名而不每次都提及变量
- html - HTML 消失?我见过的最复杂的错误
- java - 计数器在千分尺中是线程安全的吗
- ios - React-native-firebase:推送通知并不总是在 iOS 上工作
- database - 如何使用碳在一个月内获得工作日期?
- swift - 销毁 UICollectionView 中的滚动视频单元格以获得类似 TikTok 的功能
- asp.net-mvc - 尝试排序时无法翻译 LINQ 表达式
- android - 有没有可以从 firestore 获取所有文档然后使用其中的数据的方法?
- bash - 如何在 crontab 中匹配正则表达式