Kuldeep
Kuldeep

Reputation: 75

QueryFailedError: invalid input syntax for type integer: "search"

I have been doing to correct this but still getting the error

user.controller.ts

import {
  Controller,
  Get,
  Query,
  ValidationPipe,
} from '@nestjs/common';
import { UserService } from './users.service';
import { SearchUserDto } from './dto/search-user.dto';

@Controller('users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('search')
  async search(
    @Query(new ValidationPipe({ transform: true }))
    searchUserDto: SearchUserDto,
  ) {
    console.log('Transform DTO', searchUserDto);
    return this.userService.search(searchUserDto);
  }
}


user.service.ts

import {
  Injectable,
  NotFoundException,
  ConflictException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { SearchUserDto } from './dto/search-user.dto';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class UserService {
  private readonly cacheManager: CacheManager.Cache;

  constructor(
    @InjectRepository(User)
    private usersRepository: Repository<User>,
    private configService: ConfigService,
  ) {
    this.cacheManager = CacheManager.caching({
      store: redisStore,
      host: this.configService.get<string>('REDIS_HOST'),
      port: this.configService.get<number>('REDIS_PORT'),
      ttl: this.configService.get<number>('REDIS_TTL'),
    });
  }

  async search(searchUserDto: SearchUserDto): Promise<User[]> {
    console.log('search', searchUserDto);
    const queryBuilder = this.usersRepository.createQueryBuilder('user');
    console.log('query', queryBuilder);

    if (searchUserDto.username) {
      queryBuilder.andWhere('user.username LIKE :username', {
        username: `%${searchUserDto.username}%`,
      });
    }

    if (searchUserDto.minAge) {
      const minBirthdate = new Date();
      minBirthdate.setFullYear(
        minBirthdate.getFullYear() - searchUserDto.minAge,
      );
      queryBuilder.andWhere('user.birthdate <= :minBirthdate', {
        minBirthdate: minBirthdate.toISOString().split('T')[0],
      });
    }

    if (searchUserDto.maxAge) {
      const maxBirthdate = new Date();
      maxBirthdate.setFullYear(
        maxBirthdate.getFullYear() - searchUserDto.maxAge,
      );
      queryBuilder.andWhere('user.birthdate >= :maxBirthdate', {
        maxBirthdate: maxBirthdate.toISOString().split('T')[0],
      });
    }

    return queryBuilder.getMany();
  }
}

search-user.dto.ts

export class SearchUserDto {
  username?: string;
  minAge?: number;
  maxAge?: number;
}

CURL REQUEST

curl --location 'http://localhost:3000/api/users/search?username=maharaja&minAge=5&maxAge=20'

please look all over the files and let me know where am i doing wrong and explain me why? the continously getting the error is QueryFailedError: invalid input syntax for type integer: "search". sometimes I have changed the dto but it didn't work out and went through the curl parameters but still got the same error.

Upvotes: 0

Views: 38

Answers (0)

Related Questions