Nest can't resolve dependencies of the UserService (?). Please make sure that the argument "UserRepository" at index [0]

28 Views Asked by At

I am trying to develop a login flow using passport, but I am getting the following error.

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

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

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './user/user.module';
import { User } from './user/user.entity';
import { UserController } from './user/user.controller';
import { UserService } from './user/user.service';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './auth/local.strategy';

@Module({
  imports: [
    UserModule,
    PassportModule,

    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'gokitchen',
      entities: [User, Country],
      synchronize: false,
      autoLoadEntities: true,
    }),
  ],
  controllers: [AppController, UserController],
  providers: [AppService, UserService, LocalStrategy],
})
export class AppModule {}

user.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private userRepository: Repository<User>,
  ) {}

  async createUser(userData: Partial<User>): Promise<User> {
    const user = this.userRepository.create(userData);
    return await this.userRepository.save(user);
  }

  findAll(): Promise<User[]> {
    return this.userRepository.find();
  }

  findOne(email: string): Promise<User | null> {
    return this.userRepository.findOneBy({ email });
  }

  async remove(id: number): Promise<void> {
    await this.userRepository.delete(id);
  }
}

user.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { User } from './user.entity';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  providers: [UserService],
  controllers: [UserController],
  exports: [UserService],
})
export class UserModule {
  constructor(private userService: UserService) {}
}

1

There are 1 best solutions below

0
Jay McDoniel On

If you already have the UserController in the UserModule there's no need to add it again in the AppModule. Meaning you can remove both the UserController and the UserService from the AppModule's metadata arrays.

If you end up needing access to the UserService in another module, you should add the UserService to the exports of the UserModule and then add the UserModule to the imports of the module that needs the access.