简体   繁体   中英

NestJS/TypeORM: Cannot read property 'createQueryBuilder' of undefined

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM