2
votes

calling 'localhost:3000/contacts' (with or without parameters) at postman returns me this error and i don't know why. My backend is connected to a PostgreSQL db.

TypeError: Cannot read property 'createQueryBuilder' of undefined at ContactsRepository.Repository.createQueryBuilder (...\Documents\Visual Studio Code Projects\funds-backend-nestjs\node_modules\typeorm\repository\Repository.js:17:29) at ContactsRepository.getContacts (...\Documents\Visual Studio Code Projects\funds-backend-nestjs\dist\contacts\contacts.repository.js:17:34) at ContactsService.getContacts (...\Documents\Visual Studio Code Projects\funds-backend-nestjs\dist\contacts\contacts.service.js:24:39) at ContactsController.getContacts (...\Documents\Visual Studio Code Projects\funds-backend-nestjs\dist\contacts\contacts.controller.js:25:37) at ...\Documents\Visual Studio Code Projects\funds-backend-nestjs\node_modules@nestjs\core\router\router-execution-context.js:38:29 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async ...\Documents\Visual Studio Code Projects\funds-backend-nestjs\node_modules@nestjs\core\router\router-execution-context.js:46:28 at async ...\Documents\Visual Studio Code Projects\funds-backend-nestjs\node_modules@nestjs\core\router\router-proxy.js:9:17

My code looks like this:

@EntityRepository(Contact)
export class ContactsRepository extends Repository<Contact> {

  async getContacts(filterDto: GetContactsFilterDto): Promise<Contact[]> {
    const { name, search } = filterDto;
    // const query = this.createQueryBuilder('contacts');
    const query = await this.createQueryBuilder()
      .select('contacts')
      .from(Contact, 'contacts');

    if (name) {
      query.andWhere('contacts.name = :name', { name });
    }

    if (search) {
      query.andWhere(
        '(contacts.email LIKE :search OR contacts.telephone LIKE :search)',
        { search: `%${search}%` },
      );
    }

    const contacts = await query.getMany();
    return contacts;
  }
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity({ name: 'contacts' })
export class Contact extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  email: string;

  @Column()
  html_de: string;

  @Column()
  html_en: string;

  @Column()
  name: string;

  @Column()
  telephone: string;
}
export class ContactsController {
  constructor(private contactsService: ContactsService) {}

  @Get()
  getContacts(
    @Query(ValidationPipe) filterDto: GetContactsFilterDto,
  ): Promise<ContactDto[]> {
    return this.contactsService.getContacts(filterDto);
  }
@Injectable()
export class ContactsService {
  constructor(
    @InjectRepository(ContactsRepository)
    private contactsRepository: ContactsRepository,
  ) {}

  async getContacts(filterDto: GetContactsFilterDto): Promise<Contact[]> {
    return this.contactsRepository.getContacts(filterDto);
  }
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { ContactsController } from './contacts.controller';
import { ContactsRepository } from './contacts.repository';
import { ContactsService } from './contacts.service';

@Module({
  controllers: [ContactsController],
  imports: [TypeOrmModule.forFeature([ContactsRepository])],
  providers: [ContactsRepository, ContactsService],
  exports: [ContactsRepository, ContactsService],
})
export class ContactsModule {}

Somebody know how i can fix this? Regards

1
In a older version i didnt use the alias 'contacts'. Back then I named my entity 'contacts' to fit the table name 'contacts'. Wanted to make it more consistent, because an entity should be singular. It worked. Does it have something to do with it? Because right now i tried it with getManager, and it works again. const query = await getManager().createQueryBuilder(Contact, 'contacts'); github.com/typeorm/typeorm/blob/master/docs/… - axelhmm
Can you also show your ContactsModule? - Jay McDoniel
I brought it back to working using TypeORM's Connection in the service: this.contactsRepository = this.connection.getCustomRepository(ContactsRepository); Still i'm wondering why i am not able to use Dependency Injection anymore. It must be because of the Alias used in the Entity: @Entity({ name: 'contacts' }) - axelhmm
StackOverflow comments don't do multiline formatting. Can you add it to your question body instead of the comment? - Jay McDoniel
I edited my post and added the module. - axelhmm

1 Answers

3
votes

ContactsRepository should only be used in the TypeOrmModule.forFeature() and not added to the providers or exports array. When it is added here, the injection token for ContactsRepository no longer points to the proper instance and Nest creates the class, but doesn't have it properly extend Repository as that code is all managed by TypeORM