mongodb Nest无法解析项目中的依赖项

mdfafbf1  于 2023-02-03  发布在  Go
关注(0)|答案(1)|浏览(117)

控制台显示:Nest无法解析VisitService(?,DonorService)的依赖项。请确保索引[0]处的参数VisitModel在VisitModule上下文中可用。
潜在解决方案:

  • 如果VisitModel是一个提供程序,它是否是当前VisitModule的一部分?
  • 如果VisitModel是从一个单独的@Module导出的,那么该模块是否在VisitModule中导入?

这里是vistit.服务:

import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { DonorVisitStatus } from 'src/constants/donor-visit-status.enum';
import { GetDateDifferenceHours } from 'src/utils';
import { DonorService } from '../donor/donor.service';
import { VisitDocument } from '../schemas/visit.schema';
import { CreateVisitInput } from './graphql/visit.input';
import { Visit } from './graphql/visit.type';

@Injectable()
export class VisitService {
  constructor(
    @InjectModel('Visit') private readonly visitModel: Model<VisitDocument>,
    @Inject(forwardRef(() => DonorService)) private donorService: DonorService,
  ) {}

  async findVisitById(visitId: string): Promise<VisitDocument | undefined> {
    const visit = await this.visitModel
      .findOne({ _id: visitId })
      .populate('medicalCheckup')
      .populate('phlebotomy')
      .populate('donor');
    return visit || undefined;
  }

}

访问。模块:

import { Module, forwardRef } from '@nestjs/common';
import { VisitService } from './visit.service';
import { VisitResolver } from './visit.resolver';
import { ConfigModule } from 'config/config.module';
import { MongooseModule } from '@nestjs/mongoose';
import { VisitSchema } from '../schemas/visit.schema';
import { DonorModule } from '../donor/donor.module';

@Module({
  imports: [
    MongooseModule.forFeature([{ name: 'Visit', schema: VisitSchema }], 'aidea_donation'),
    ConfigModule,
    forwardRef(() => DonorModule),
  ],
  providers: [VisitService, VisitResolver],
  exports: [VisitService],
})
export class VisitModule {}

我已经检查了很多,但我找不到任何错误

iecba09b

iecba09b1#

您在MongooseModule.forFeature()中使用了命名连接,因此需要在@InjectModel()中使用相同的连接。将@InjectModel()更改为@InjectModel('Visit', 'aidea_donation'),它应该可以工作

相关问题