#Nest can't resolve dependencies of the UsersRepository

1 messages · Page 1 of 1 (latest)

worn citrus
#

I've solved this before but a bit stuck this time so figured I'd ask for help


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

For additional context, this is occurring in authMigrationDeps.module.ts, which looks like this:

@Module({
  imports: [AuthDatabaseModule],
  providers: [
    ConfigService,
    UsersRepository,
    AuthMigrationService,
  ],
})
export class AuthMigrationDepsModule {}

users.repository.ts:

@Injectable()
export class UsersRepository extends AbstractRepository<UserEntity> {
  constructor(
    @Optional()
    @InjectDataSource('authDB')
    connection: DataSource,
    @InjectRepository(UserEntity, 'authDB')
    private userEntity: Repository<UserEntity>,
    private walletRepository: WalletsRepository,
  ) {
    super(
      userEntity ?? connection.getRepository(UserEntity),
      undefined,
      connection,
    );
  }
... 
}

And authMigration.service.ts, where all of this is being used:

@Injectable()
export class AuthMigrationService {
  constructor(
    @InjectDataSource('authDB')
    private authDataSource: DataSource,
    @Optional()
    @InjectRepository(WalletAddressEntity, 'authDB')
    private walletEntity: Repository<WalletAddressEntity>,
    @Optional() private walletsRepository: WalletsRepository,
    @Optional()
    @InjectRepository(UserEntity, 'authDB')
    private userEntity: Repository<UserEntity>,
    @Optional() private usersRepository: UsersRepository,
  ) {
    ...
  }

Also if there are any resources to learn this stuff better, in particular relating to typeorm and datasources, I would love to know as I'm still very confused about the flow of importing/exporting modules and providers. Thanks in advance

chrome basin
#

when you import a module, you're importing the providers that the exports. That's all you need to know, I guess

#

when the module is global, its providers are globally available

#

looks like you've missed some import at AuthMigrationDepsModule

#

please show us that AuthDatabaseModule module

worn citrus
#

Gladly @chrome basin , thanks for the quick reply:

authDatabase.module.ts

@Module({
  providers: [
    {
      provide: 'authDBDataSource',
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        const datasource = new DataSource({
          type: 'postgres',
          port: config.get<number>('db.auth.port'),
          synchronize: false,
          host: config.get<string>('db.auth.host'),
          username: config.get<string>('db.auth.username'),
          password: config.get<string>('db.auth.password'),
          database: config.get<string>('db.auth.database'),
          entities: [
            UserEntity,
            WalletAddressEntity,
            RoleEntity,
            PermissionEntity,
            RolePermissionEntity,
            LoginAttemptEntity,
          ],
          migrationsRun: false,
          namingStrategy: new SnakeNamingStrategy(),
        });

        return await datasource.initialize();
      },
    },
    UsersRepository,
  ],
  imports: [ConfigModule, TypeOrmModule],
  exports: [TypeOrmModule, 'authDBDataSource', UsersRepository],
})
export class AuthDatabaseModule {}
chrome basin
#

why do you have UsersRepository registered in both modules?

#

I guess you don't need the one at AuthMigrationDepsModule

#

as you've exported UsersRepository from AuthDatabaseModule already

worn citrus
#

Ah, I was just trying random stuff to try and get it working, and you're seeing the result of that. I will try removing from AuthMigrationDepsModule

#

I've removed it from AuthMigrationDepsModule, but still getting the same error

chrome basin
#

I don't think it's the exact same error

#

show us the error msg

worn citrus
#
UnknownDependenciesException [Error]: Nest can't resolve dependencies of the UsersRepository (authDBDataSource, ?, WalletsRepository). Please make sure that the argument authDB_UserEntityRepository at index [1] is available in the AuthDatabaseModule context.

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

You're right, my mistake. It's in AuthDatabaseModule now

chrome basin
#

is that TypeOrmModule from @nestjs/typeorm?

worn citrus
#

Yes it is

#

import { TypeOrmModule } from '@nestjs/typeorm';

chrome basin
#

either TypeOrmModule.forRoot or TypeormOrmModule.forFeature
depending on what you're trying to do

#

just follow that docs and you should be fine 🙂