Passing an array as a parameter in a WHERE IN clause

Viewed 59

I am trying pass in an array of strings (internal_id) to a query. The portion below counts how many elements are in the internal_id array and parameterizes them into a string so it can be compatible with SQL(Ex. if there are two IDs in internal_id, then paramString would be "$1,$2")

const { internal_id } = args;  // This is an array of strings
let paramString: Array<string> = [];
for (let count = 1; count <= internal_id!.length; count++) {
  paramString.push(`$${count}`)
}

let queryString = `
  SELECT *
  FROM customer
  WHERE internal_id IN (${paramString.join(',')})`

Then I tried to pass the internal_id array as a parameter below:

const res = await db.get(queryString, [internal_id]);

But I get this error

error: bind message supplies 1 parameters, but prepared statement "" requires 2

I am not sure how to correctly pass internal_id array through db.get().

1 Answers

You provide and array containing another array internal_id instead of just internal_id itself.

Related