0

I'm new to NestJs environement , and Im currently facing this error , and looked for the issues solutions on github but couldn't help me on getting a result to my error.

[Nest] 22140   - 21/06/2021 à 14:51:40   [ExceptionHandler] Nest can't resolve dependencies of the UsersRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.

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

And I'm already declaring it on my auth.module.ts

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersRepository } from './users.repository';

@Module({
  imports: [TypeOrmModule.forFeature([UsersRepository])],
  providers: [AuthService],
  controllers: [AuthController],
})
export class AuthModule {}


and using it on my service file .

import { Injectable } from '@nestjs/common';
import { UsersRepository } from './users.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Injectable()
export class AuthService {
    constructor (
        @InjectRepository(UsersRepository)
        private usersRepository: UsersRepository,
    ) {}

    async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
        return this.usersRepository.createUser(authCredentialsDto);
    }
}

  • You have `TypeOrmModule.forRoot/Async()` in your `AppModule`, right? How are you running the code that's generating the error? – Jay McDoniel Jun 21 '21 at 16:10

1 Answers1

0

just use your UsersRepository

example


import { Injectable } from '@nestjs/common';
import { UsersRepository } from './users.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Injectable()
export class AuthService {
    constructor (
        private usersRepository: UsersRepository,
    ) {}

    async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
        return this.usersRepository.createUser(authCredentialsDto);
    }
}

If you have custom repository you don't need InjectRepository because it returns default repository for entity which you passed in

mistamax
  • 71
  • 1
  • 1
  • hello , even with this solution the error is persisting saying : `[Nest] 24504 - 22/06/2021 à 06:28:55 [ExceptionHandler] Nest can't resolve dependencies of the UsersRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.` –  Jun 22 '21 at 04:35
  • Maybe this article helps: [nest-js-cant-resolve-dependencies](https://stackoverflow.com/a/60209470/1598584). – DLade Jun 01 '23 at 06:33