How to perform a like query Typeorm

Gardezi picture Gardezi · Dec 14, 2017 · Viewed 27.3k times · Source

Hello guys I'm trying to find all the results that have a in them. I have tried a couple of ways but the problem is nothing works. It just returns an empty array

  var data = await  getRepository(User)
                        .createQueryBuilder("user")
                        .where("user.firstName = %:name%", {name: firstName })
                .getMany();

and something like this

 var data = await  getRepository(User)
                            .createQueryBuilder("user")
                            .where("user.firstName like %:name%", {name: firstName })
                    .getMany();

but nothing is working. All of these are returning me a empty array. Can somebody help me out thanks

Answer

pleerock picture pleerock · Dec 15, 2017

Correct way is:

 var data = await getRepository(User)
                  .createQueryBuilder("user")
                  .where("user.firstName like :name", { name:`%${firstName}%` })
                  .getMany();