Node-postgres: Passing parameters to `IN`

Created on 19 Jun 2020  路  4Comments  路  Source: brianc/node-postgres

Heya, how would one go about passing parameters to a query with IN?

For example:

SELECT *
FROM people
WHERE name in ($1)

I've tried to use:

pg.query(query, [['drake', 'zoey']], cb)

But I don't get results back when both drake and zoey are in the table.

However the following does seem to work:

pg.query(query.replace(/\$1/, ['drake', 'zoey'].map(v => `'${v}'`).join(','), cb)
question

Most helpful comment

You can use = ANY instead:

SELECT *
FROM people
WHERE name = ANY ($1)

All 4 comments

You can use = ANY instead:

SELECT *
FROM people
WHERE name = ANY ($1)

Duplicate of #1452

However the following does seem to work:

pg.query(query.replace(/\$1/, ['drake', 'zoey'].map(v => `'${v}'`).join(','), cb)

(and, to be clear, definitely do not do this. It鈥檚 an SQL injection vulnerability.)

<3 thank you

Was this page helpful?
0 / 5 - 0 ratings