The like query of typeorm is not working. Here is the code where I try to search by keyword in email
async findAll(findAllQuery: FindAllQuery): Promise<any> {
const [users, total] = await this.usersRepository.findAndCount(
{
where: {
email: Like(`%${findAllQuery.keyword}%`),
},
skip: findAllQuery.page - 1,
take: findAllQuery.itemsPerPage,
},
);
return { users, total };
}
Any ideas how to get this work?
I finally did it work avoiding Like, it seems I misunderstood the meaning of Like, all I wanted was to search in users by keyword, so I did something like this, and it worked.
async findAll(findAllQuery: FindAllQuery): Promise<any> {
const options: FindManyOptions<User> = {};
if(findAllQuery.keyword){
options.where = {
$or:[
{email: RegExp(`^${findAllQuery.keyword}`,'i')},
{firstName: RegExp(`^${findAllQuery.keyword}`,'i')},
{lastName: RegExp(`^${findAllQuery.keyword}`,'i')},
{username: RegExp(`^${findAllQuery.keyword}`,'i')},
],
}
}
if(findAllQuery.page){
options.skip = findAllQuery.page - 1;
}
if(findAllQuery.itemsPerPage){
options.take = findAllQuery.itemsPerPage;
}
const [users, total] = await this.usersRepository.findAndCount(options);
return { users, total };
}
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.