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) {}
}
If you already have the
UserControllerin theUserModulethere's no need to add it again in theAppModule. Meaning you can remove both theUserControllerand theUserServicefrom theAppModule's metadata arrays.If you end up needing access to the
UserServicein another module, you should add theUserServiceto theexportsof theUserModuleand then add theUserModuleto theimportsof the module that needs the access.