#Probably injection problems - Ref* and ChildClass problem

7 messages · Page 1 of 1 (latest)

mild bone
#
@Injectable()
export class UserAccountService {
  constructor(
    @InjectRepository(UserAccount)
    private readonly userAccountRepository: UserAccountRepository,
    private configService: ConfigService,
    private userEmailVerificationService: UserEmailVerificationService,
  ) {}

This is my service, it has a lot of methods, that works well:

  async findOneByEmail(email: string) {
    return await this.userAccountRepository.findOneUserAccountByEmail(email);
  }

That works well inside the UserAccountModule:

@Module({
  controllers: [UserAccountController, UserEmailVerificationController],
  imports: [
    TypeOrmModule.forFeature([
      UserAccount,
      UserPermissionsDescriptors,
      AuthorizationPermission,
      UserAccountEmailVerification,
    ]),
    NotificationModule,
    AuthorizationModule,
  ],
  providers: [
    UserEmailVerificationService,
    NotificationService,
    AuthorizationService,
    {
      provide: getRepositoryToken(UserAccount),
      inject: [getDataSourceToken()],
      useFactory(datasource: DataSource) {
        return datasource
          .getRepository(UserAccount)
          .extend(UserAccountRepositoryImpl);
      },
    },
    {
      provide: getRepositoryToken(UserAccountEmailVerification),
      inject: [getDataSourceToken()],
      useFactory(datasource: DataSource) {
        return datasource
          .getRepository(UserAccountEmailVerification)
          .extend(UserAccountEmailVerificationRepositoryImpl);
      },
    },
    UserAccountService,
  ],
  exports: [UserAccountService, UserEmailVerificationService],
})
export class UserAccountModule {}
#

But when I call the findOneByEmail(email: string)

From AuthService:

#
@Injectable()
export class AuthService {
  constructor(
    private userService: UserAccountService,
    private jwtService: JwtService,
    private configService: ConfigService,
  ) {}

  async tryAuthenticate(email: string, password: string) {
    const account = await this.userService.findOneByEmail(email);

It throws me an error, saying that the in line:

    return await this.userAccountRepository.findOneUserAccountByEmail(email);

The userAccountRepository doesn't have the function findOneUserAccountByEmail.

#

So I decided to print out what is the userAccountRepository in different contexts, When I call it directly from UserAccountService:

The type of userAccountRepository is:

ChildClass {
  target: [class UserAccount],
  '@transactional/entity-manager': EntityManager {
    '@instanceof': Symbol(EntityManager),

But when I was calling the from the AuthService the method from UserAccountService:

#
@Module({
  controllers: [AuthController],
  providers: [
    AuthService,
    UserAccountService,
    { provide: APP_GUARD, useClass: AuthTokenGuard },
  ],
  imports: [
    JwtModule.register({
      global: true,
      signOptions: { expiresIn: '1d' },
    }),
    TypeOrmModule.forFeature([UserAccount]),
    UserAccountModule,
  ],
})
export class AuthModule {}
#

I'm thinking that this can be related to circular dependency, right?