OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Kuldeep
  • Start date Start date
K

Kuldeep

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

user.controller.ts

Code:
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

Code:
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

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

CURL REQUEST

Code:
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.

<p>I have been doing to correct this but still getting the error</p>
<p><code>user.controller.ts</code></p>
<pre><code>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);
}
}


</code></pre>
<p><code>user.service.ts</code></p>
<pre><code>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();
}
}
</code></pre>
<p><code>search-user.dto.ts</code></p>
<pre><code>export class SearchUserDto {
username?: string;
minAge?: number;
maxAge?: number;
}
</code></pre>
<p><code>CURL REQUEST</code></p>
<pre><code>curl --location 'http://localhost:3000/api/users/search?username=maharaja&minAge=5&maxAge=20'
</code></pre>
<p>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.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top