How to convert query ' WHERE IN' of strings with TypeORM Query Builder?

Viewed 1354

Getting errors in following TypeORM query:

async exportUsers(stateId: string, zipcodes: string) 
{
  //zipcodes = '60563', '54656', '94087';//
  const query = await this.userRepository
  .createQueryBuilder('user')
  .select('user.email', 'email')
  .where('left(user.Zip,5) in :zip', { zip: zipcodes }); 
}

How to pass string containing 'array of strings' to TypeORM query using IN.

2 Answers

This worked:

zipcodes = '60563', '54656', '94087';
const ziplist: string[] = zipcodes.replace("'", "").split(",");

Changed query to:

.where('left(s.ShipToZip,5) in (:...zip)', { zip: ziplist }); 

Replace your where clause with this code:

.where("left(user.Zip,5) IN (:zip )", { zip : zipcodes })

Note: your zipcode variable should be in array format; in means:

zipcodes = ['94085','54656','94087']
Related