简体   繁体   中英

Nestjs can't resolve dependencies of XModel

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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