是否可以将多个MongoDB模型注入到一个解析器中,然后使用它们?
我试着这样做:
首先我补充说
节模式
和
部门服务
导入到
后模块
@Module({
imports: [MongooseModule.forFeature([{name: 'Post', schema: PostSchema}, {name: 'Section', schema: SectionSchema}])],
providers: [PostsResolver, PostsService, SectionsService],
})
export class PostsModule {}
然后我将所有模式导入添加到
分段模块
就像这样:
@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 {}
最后,我把所有这些模型都注入了我的
部门服务
@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 ...
}
当我试图用
npm run start:dev
我得到了这个错误:
Nest无法解析SectionsService(SectionModel、PostModel、TutorialModel)的依赖项。请确保索引[2]处的参数ProjectModel在PostsModule上下文中可用。
有可能注入许多MongoDB模型吗?
解析属性
@ResolveProperty(() => [SectionEntity])
async sections(@Parent() { _id }: PostEntity): Promise<SectionEntity[]> {
const posts: any = await this.postsService.findAll();
return this.sectionsService.findAll(_id, ArticleType.POST);
}