Nestjs can't resolve dependencies of XModel

孤街醉人 提交于 2021-01-29 17:16:53

问题


I have been working on this app for like 3 months which is near completion. But since yesterday I have been unable to solve this problem. I want to use activityTypeService in activityLogService and I have been getting this wired error. I have already exported activityTypeservice in its module. see below

below is ActivityTypeModule, I export ActivityTypeService so it can available in ActivityLogService

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

The code below is activityLog module and ActivityTypeModule is imported

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

so I use it the activityLogService as shown below

@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()
    }
}

But I am still getting this wired error which I dont understand

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 */ ]
  })


回答1:


From your error, somewhere you have ActivityTypeService in an imports array somewhere, which shouldn't be happening. Along with that, I can see you have ActivityTypeModule in a providers array, which is also not supported, though Typescript doesn't have a good way to show errors about that, so you aren't seeing any errors.

In general, as Nest error like that is in the form of

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> and <module> should never be the same value, and if they are, it's a good indication that you have a provider in an imports array.



来源:https://stackoverflow.com/questions/62579054/nestjs-cant-resolve-dependencies-of-xmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!