0
votes

This one is driving me nuts!

I have a nestjs project using typeorm, with the following simplified structure:

+ src
   + dal
        + entities
             login.entity.ts
             password.entity.ts             
        + repositories
             login.repository.ts
             password.repository.ts
        dal.module.ts
   + modules
        + security
             + services
                  login.service.ts
                  security.service.ts
             security.module.ts
   app.module.ts
   main.ts

dal defines the following custom repositories:

@EntityRepository(Login)
export class LoginRepository extends AbstractRepository<Login> implements ILoginRepository { }

@EntityRepository(Password)
export class PasswordRepository extends AbstractRepository<Password> implements IPasswordRepository { }

dal.module.ts:

@Module({
    imports: [TypeOrmModule.forFeature([Entity1, Entity2, Entity3])],
    controllers: [],
    providers: [PasswordRepository, LoginRepository],
})
export class DalModule { }

security.module.ts:

@Module({
    imports: [TypeOrmModule.forFeature([PasswordRepository, LoginRepository])],
    controllers: [SecurityController],
    providers: [LoginService, SecurityService],
})
export class SecurityModule { }

app.module.ts:

@Module({
   imports: [
      DalModule,
      SecurityModule,
      TypeOrmModule.forRoot({
         type: 'mysql',
         port: Number(process.env.DB_PORT),
         host: process.env.DB_SERVER,
         username: process.env.DB_USERNAME,
         password: process.env.DB_PASSWORD,
         database: process.env.DB_NAME,
         entities: [__dirname + '/**/*.entity{.ts,.js}'],
         synchronize: false
      })
   ]
})
export class AppModule {}

login.service.ts:

export class LoginService {
   constructor(
      private readonly passwordRepository: PasswordRepository,
      @InjectRepository(Login) private readonly loginRepository: LoginRepository
   ) {
       console.log(this.passwordRepository.f1);
       console.log(this.loginRepository.f2);
   }

Here's the thing:

The way it is now I will get [AsyncFunction: f1] and [AsyncFunction: f2] logged as expected in the LoginService constructor.

However, if I remove @InjectRepository(Login) from the second argument, I get a Cannot read property 'f1' of undefined from the first console.log. If I then comment out the first console.log, I get a Cannot read property 'f2' of undefined from the second console.log.

On the other hand, if I add @InjectRepository(Password) to the first argument, I get a Nest can't resolve dependencies of the LoginService (?, LoginRepository). Please make sure that the argument PasswordRepository at index [0] is available in the SecurityModule context error.

why on earth is this happening?

1

1 Answers

0
votes

I encountered the same problem, but I resolved it finally.

For your case, just declare Repositoryclasses as part of TypeOrmModule, and exports TypeOrmModule.

@Module({
    imports: [TypeOrmModule.forFeature([PasswordRepository, LoginRepository])],
    exports: [TypeOrmModule],
    controllers: [SecurityController],
    providers: [LoginService, SecurityService],
})
export class SecurityModule { }

When importing SecuirtyModule in other modules, the LoginRepository will be available for injection.