问题
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