Ba Loc Dang
Ba Loc Dang

Reputation: 3

Nest can't resolve dependencies of the UserService (?). Please make sure that the argument at index [0] is available in the current context

Having some problems, di/circular-references I'm sure I'm doing wrong, I just can't see it.

Any help would be much appreciated

user.module.ts

import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { PrismaService } from 'src/prisma/prisma.service';

@Module({
  controllers: [UserController],
  providers: [UserService, PrismaService],
})
export class UserModule {}

user.service.ts

import { Injectable } from '@nestjs/common';
import type { PrismaService } from 'src/prisma/prisma.service';
import type { CreateUserDto } from './dto/create-user.dto';
import { hash } from 'argon2';

@Injectable()
export class UserService {
  constructor(private readonly prisma: PrismaService) {}

  async create(createUserDto: CreateUserDto) {
    const { password, ...user } = createUserDto;
    const hashedPassword = await hash(password);
    return await this.prisma.users.create({
      data: {
        password: hashedPassword,
        ...user,
      },
    });
  }

  async findByEmail(email: string) {
    this.prisma.users.findUnique({
      where: {
        email,
      },
    });
  }
}

auth.module.ts

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UserService } from 'src/user/user.service';
import { PrismaService } from 'src/prisma/prisma.service';

@Module({
  controllers: [AuthController],
  providers: [AuthService, UserService, PrismaService],
})
export class AuthModule {}

auth.service.ts

import {
      ConflictException,
      forwardRef,
      Inject,
      Injectable,
    } from '@nestjs/common';
    import type { CreateUserDto } from 'src/user/dto/create-user.dto';
    import { UserService } from 'src/user/user.service';
    
    @Injectable()
    export class AuthService {
      constructor(
        @Inject(forwardRef(() => UserService))
        private readonly userService: UserService,
      ) {}
    
      registerUser(createUserDto: CreateUserDto) {
        const user = this.userService.findByEmail(createUserDto.email);
        if (user) throw new ConflictException('User already exists!');
        return this.userService.create(createUserDto);
      }
    }

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma/prisma.service';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';

@Module({
  imports: [AuthModule, UserModule],
  controllers: [AppController],
  providers: [AppService, PrismaService],
})
export class AppModule {}
ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserService (?). Please make sure that the argument Function at index [0] is available in the AuthModule context.

Potential solutions:
- Is AuthModule a valid NestJS module?
- If Function is a provider, is it part of the current AuthModule?
- If Function is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing Function */ ]
  })

Error: Nest can't resolve dependencies of the UserService (?). Please make sure that the argument Function at index [0] is available in the AuthModule context.

Potential solutions:
- Is AuthModule a valid NestJS module?
- If Function is a provider, is it part of the current AuthModule?
- If Function is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing Function */ ]
  })

Was originally having "Nest can't resolve dependencies of the UserService. I then deleted the UserModule entirely and just used the AuthModule, everything worked. Can you tell me where I am wrong. Thanks in advance.

Upvotes: 0

Views: 64

Answers (2)

Motunrayo Koyejo
Motunrayo Koyejo

Reputation: 397

The issue is because UserService is being used in both UserModule and AuthModule. However, UserService is not explicitly exported from the UserModule and imported into the AuthModule. Also, defining UserService in both UserModule and AuthModule could lead to conflicts.

in your UserModule, add exports: [UserService] just below the providers section. In your AuthModule, Import the UserModule, then remove the UserService from the provider.

Your user.module.ts should now look like this -

@Module({
  controllers: [UserController],
  providers: [UserService, PrismaService],
  exports: [UserService], 
})

Your auth.module.ts should look like this --

@Module({
  imports: [UserModule],
  controllers: [AuthController],
  providers: [AuthService, PrismaService],
})

Upvotes: -1

Zulkefal
Zulkefal

Reputation: 34

There is a circular dependency.

constructor(@Inject(forwardRef(() => UserService)) private readonly userService: UserService) {}

add this line in your service of auth. (Auth.service.ts)

and try to share the code of AuthServices too.

also export the services from your every module.

@Module({
controllers: [UserController],
providers: [UserService, PrismaService],
exports:[UserService]
})
export class UserModule {}

do same for other modules too

Upvotes: 0

Related Questions