首页 > 解决方案 > 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);
  }

标签: mongodbtypescriptmongoosenestjs

解决方案


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


推荐阅读