【问题标题】:Nestjs can't resolve dependencies of XModelNestjs 无法解析 XModel 的依赖
【发布时间】:2020-06-26 01:39:41
【问题描述】:

我已经在这个应用程序上工作了大约 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 */ ]
  })

【问题讨论】:

  • 你能尝试从ActivityLogModuleproviders数组中删除ActivityTypeModule吗?
  • 这样做但有同样的错误

标签: node.js angular typescript module nestjs


【解决方案1】:

根据您的错误,您在某处的某个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.

&lt;Provider&gt;&lt;module&gt; 绝不应该是相同的值,如果是,则说明您在导入数组中有提供程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2018-11-22
    • 2019-12-19
    • 2018-12-25
    • 2019-01-21
    • 2022-01-25
    相关资源
    最近更新 更多