繁体   English   中英

Nestjs 无法解析 XModel 的依赖关系

[英]Nestjs can't resolve dependencies of XModel

我已经在这个应用程序上工作了 3 个月,接近完成。 但是从昨天开始,我一直无法解决这个问题。 我想在activityLogService 中使用activityTypeService,但我一直收到这个有线错误。 我已经在其模块中导出了 activityTypeservice。 见下文

下面是 ActivityTypeModule,我导出 ActivityTypeService 以便它可以在 ActivityLogService 中使用

@Module({
  imports: [MongooseModule.forFeature([{ name: 'ActivityType', schema: ActivityTypeSchema },])],
  providers: [ActivityTypeService,],
  controllers: [ActivityTypeController],
  exports: [ActivityTypeService,]
})
export class ActivityTypeModule { }

下面的代码是activityLog模块并导入了ActivityTypeModule

@Module({
  imports: [MongooseModule.forFeature([{ name: 'ActivityLog', schema: ActivityLogSchema }]), ActivityTypeModule],
  providers: [ActivityLogService, ActivityTypeModule],
  controllers: [ActivityLogController],
  exports: [MongooseModule,]
})
export class ActivityLogModule { }

所以我使用它的activityLogService,如下所示

@Injectable()
export class ActivityLogService {
    constructor(@InjectModel('ActivityLog') private activitylogModel: Model<ActivityLog>,
        private activityTypeService: ActivityTypeService
    ) { }

    async activitylog(activityuser: string, activitytype: string, activitydetails: string, activitydate: Date) {
        const activitiesLog = {
            activityuser: activityuser,
            activitytype: activitytype,
            activitydetails: activitydetails,
            activitydate: activitydate
        }
        const activity = new this.activitylogModel(activitiesLog);
        console.log(activity);
        await activity.save()
    }
}

但我仍然收到这个我不明白的有线错误

Nest can't resolve dependencies of the ActivityTypeService (?). Please make sure that the argument ActivityTypeModel at index [0] is available in the ActivityTypeService context.

Potential solutions:
- If ActivityTypeModel is a provider, is it part of the current ActivityTypeService?
- If ActivityTypeModel is exported from a separate @Module, is that module imported within ActivityTypeService?
  @Module({
    imports: [ /* the Module containing ActivityTypeModel */ ]
  })

根据您的错误,您在某个地方的imports数组中有ActivityTypeService ,这不应该发生。 除此之外,我可以看到您在providers数组中有ActivityTypeModule ,虽然 Typescript 没有显示错误的好方法,但您没有看到任何错误。

一般来说,像这样的 Nest 错误的形式是

Nest can't resolve dependencies of the <Provider> (?). Please make sure that the argument <injected_valued> at index [<index>] is available in the <module> context.

<Provider><module>不应该是相同的值,如果它们是,这很好地表明您在导入数组中有一个提供程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM