markWanka
markWanka

Reputation: 784

Second Like in Find Query

How to use second LIKE in typeorm find query? I try like below, but not work and always return empty array

        return await this.conn.getRepository(Article).find({
            where: {
                body: Like(`%${query}%`),
                //title: Like(`%${query}%`), //NOT WORK WITH SECOND
            },
            order: {
                lastUpdate: 'DESC'
            },
            relations: ['author']
        }); 

thanks for any help!

Upvotes: 0

Views: 39

Answers (1)

David Tansey
David Tansey

Reputation: 6023

I have to make a lot assumptions about your data and use case.

I suspect that the problem you are having has to do with the boolean operation that gets generated by TypeORM for your where condition(s).

Based on the way you have specified your request the resulting boolean operation is an AND. This means it will only return TRUE if both body AND title contain the search string.

I suspect this is NOT what you intended. I am assuming that what you really want is for the where to return TRUE if body or title or both contain the search string.

If all my assumptions are all correct, then you should be able to get the result you want by changing your request to the following:

return await this.conn.getRepository(Article).find({
    where: [
        {body: Like(`%${query}%`)},
        {title: Like(`%${query}%`)} 
    ],
    order: {
        lastUpdate: 'DESC'
    },
    relations: ['author']
}); 

Using this form of the request should result in a where clause that use an OR operator between the two Like tests.

Upvotes: 1

Related Questions