Anton Bohomaz
Anton Bohomaz

Reputation: 99

Nestjs can't resolve my dependency even though it's available in Module context

I get such error:

Error: Nest can't resolve dependencies of the CreateGroupTask (TaskQueueService, GroupsService, ?, GroupNotificationsService, GroupRepository, Logger). Please make sure that the argument dependency at index [2] is available in the TasksModule context.

CreateGroupTasks service looks this way:

import { Logger } from 'nestjs-pino';
import { GroupNotificationsService } from '../bll/group/group-notifications.service';
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { GroupsService } from '../bll/group/groups.service';
import { PotentialGroupsService } from '../bll/potential-groups/potential-groups.service';
import { GroupRepository } from '../dal/user-group/group.repository';
import { TaskQueueService } from '../task-queue/task-queue.service';

export const CREATE_GROUP_TASK_NAME = 'create-group';
const CREATE_GROUP_TASK_DOMAIN = 'create-group-task';
@Injectable()
export class CreateGroupTask implements OnApplicationBootstrap {
  constructor(
    private readonly taskQueueService: TaskQueueService,
    private readonly groupsService: GroupsService,
    private readonly potentialGroupService: PotentialGroupsService,
    private readonly groupNotificationsService: GroupNotificationsService,
    private readonly groupsRepository: GroupRepository,
    private readonly logger: Logger,
  ) {}

  onApplicationBootstrap() {
    this.taskQueueService.registerTaskHandler(
      CREATE_GROUP_TASK_NAME,
      this.handle.bind(this),
    );
  }

  async handle([potentialGroupId]: [number]) { //handler code }
}

PotentialGroupsService exported from BusinessLogicModule module as well as GroupsService, and GroupNotificationsService:

import { Module } from '@nestjs/common';
import { SendAuthLinkTask } from './send-auth-link.task';
import { CallReminderTask } from './call-reminder-task';
import { TaskQueueModule } from '../task-queue/task-queue.module';
import { RepositoryModule } from '../dal/repository.module';
import { CommunicationsgModule } from '../communication/communications.module';
import { BusinessLogicModule } from '../bll/business-logic.module';
import { SendStatsTask } from './send-stats.task';
import { WorkoutNotificationTask } from './workout-notification.task';
import { SayWelcomeTask } from './say-welcome.task';
import { CreateGroupTask } from './create-group.task';
import { GroupStartsReminderTask } from './group-starts-reminder.task';

@Module({
  imports: [
    TaskQueueModule,
    CommunicationsgModule,
    RepositoryModule,
    BusinessLogicModule,
  ],
  providers: [
    SendAuthLinkTask,
    CallReminderTask,
    SendStatsTask,
    WorkoutNotificationTask,
    SayWelcomeTask,
    CreateGroupTask,
    GroupStartsReminderTask,
  ],
})
export class TasksModule {}

And all those services exported from the BusinessLogicModule:

import { Module } from '@nestjs/common';
import { CommunicationsgModule } from '../communication/communications.module';
import { RepositoryModule } from '../dal/repository.module';
import { AuthService } from './auth-logic/auth.service';
import { SessionService } from './auth-logic/session.service';
import { GroupNotificationsService } from './group/group-notifications.service';
import { UserService } from './user/user.service';
import { MessageService } from './conversation/message.service';
import { ConversationService } from './conversation/conversation.service';
import { GroupsService } from './group/groups.service';
import { StatsService } from './statistics/workouts-stats';
import { InvitationsService } from './invitations/invitations.service';
import { TaskValidator } from './group/notification-task-validator.service';
import { PotentialGroupsService } from './potential-groups/potential-groups.service';
import { PotentialGroupCheckerCron } from './potential-groups/potential-groups-checker.cron';

@Module({
  imports: [CommunicationsgModule, RepositoryModule],
  providers: [
    AuthService,
    SessionService,
    GroupNotificationsService,
    UserService,
    MessageService,
    ConversationService,
    GroupsService,
    StatsService,
    InvitationsService,
    TaskValidator,
    PotentialGroupsService,
    PotentialGroupCheckerCron,
  ],
  exports: [
    AuthService,
    SessionService,
    GroupNotificationsService,
    UserService,
    MessageService,
    ConversationService,
    GroupsService,
    StatsService,
    InvitationsService,
    TaskValidator,
    PotentialGroupsService,
  ],
})
export class BusinessLogicModule {}

What do I do wrong? Why nest can't resolve PotentialGroupService?

Upvotes: 0

Views: 3175

Answers (2)

Nika Andriadze
Nika Andriadze

Reputation: 19

It can't resolve PotentialGroups.

Make sure that PotentialGroupsModule is imported inside BusinessLogicModule. And also make sure that PotentialGroupsService is exported from PotentialGroupsModule

Upvotes: 0

Jay McDoniel
Jay McDoniel

Reputation: 70061

From the error:

Error: Nest can't resolve dependencies of the CreateGroupTask (TaskQueueService, GroupsService, ?, GroupNotificationsService, GroupRepository, Logger). Please make sure that the argument dependency at index [2] is available in the TasksModule context.

Nest can't resolve the argument dependency. Usually when this happens there is a circular file import that can't be resolved, meaning the dependency comes in as undefined, so Nest provides a generic name for the dep. Check your file imports (including barrel files) and make sure you're not creating a circular import on accident.

Upvotes: 2

Related Questions