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.
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