Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS y TypeORM: InjectRepository undefined

I have a problem in a service that I am developing with nestjs and TypeORM

The system runs with a docker, everything works fine on my pc, but when I run it on any other side it stops working.

The error says:

TypeError: Cannot read property 'findOne' of undefined

BasicRepository:

export abstract class BasicRepository<Entity> {

  constructor(protected readonly repository: Repository<Entity>,
protected readonly request: Request) {
     console.log("basicRepo", this.repository.metadata.givenTableName) 
  }
}

UserRepository:

import { Inject, Injectable, Scope } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { BasicRepository } from './basic.repository';
import { UserEntity } from '../../shared/entities/user.entity';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';

@Injectable({ scope: Scope.REQUEST })
export class UserRepository extends BasicRepository<UserEntity> {

  constructor(
    @InjectRepository(UserEntity) public readonly repository: Repository<UserEntity>,
    @Inject(REQUEST) public readonly request: Request,
  ) {
    super(repository, request);
  }
}

TapRepository:

import { Inject, Injectable, Scope } from '@nestjs/common';
import { Repository, FindManyOptions, getConnection, getManager } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { BasicRepository } from './basic.repository';
import { TapEntity} from '../../shared/entities/tap.entity';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';

@Injectable({ scope: Scope.REQUEST })
export class TapRepository extends BasicRepository<TapEntity> {

  constructor(
    @InjectRepository(TapEntity) public readonly repository: Repository<TapEntity>,
    @Inject(REQUEST) public readonly request: Request,
  ) {
    super(repository, request);
  }
}

The strange thing is that in this case UserRepository does not give me an error, but the others.

The system only works on my pc, I usually run the system using kubernetes, it fails when I want to run it on GKE (google) or EKS (amazon).

Another thing that happens is that the 'basicRepo' message that prints the name of the repository table is only printed with the UserRepository class.

like image 940
Francuchin Avatar asked Jan 20 '26 03:01

Francuchin


1 Answers

Solution is simple instead of using @InjectRepository() use this Connection from typeorm to get repository like this and initialize.

export class UserService {
    private userRepository: UserRepository;
    private authRepository: AuthRepository;
    constructor(
        private readonly connection: Connection
    ) {
        this.userRepository = this.connection.getCustomRepository(UserRepository);
        this.authRepository = this.connection.getCustomRepository(AuthRepository);
    }
}

Now it work like charm :)

like image 54
arslanmughal Avatar answered Jan 23 '26 20:01

arslanmughal