So, I’m trying to build a query in Nodejs, specifically using Express.js router. But I’m having troubles with the “IN” command when I try to put my parameters:
This code doesn’t work:
router.get(‘/’, async (req, res) => {
try {
const foo = ‘1,2,3’ //localhost:5000/sample?category_id=1,2,3
const query = sql`select * from categories ${ foo ? sql`where category_id in (${foo})` : sql`` }`;
console.log(query);
const result = await query;
res.send(result);
} catch (error) {
console.error(error);
res.status(500).send(‘Internal server error’);
}
});
If I try to just statically type the parameters, it works:
router.get(‘/’, async (req, res) => {
try {
const foo = ‘1,2,3’ //localhost:5000/sample?category_id=1,2,3
const query = sql`select * from categories ${ foo ? sql`where category_id in (1,2,3)` : sql`` }`;
console.log(query);
const result = await query;
res.send(result);
} catch (error) {
console.error(error);
res.status(500).send(‘Internal server error’);
}
});
Sorry, if I missed something basic, but for my naive brain, it seems like it’s just a simple replace this with that then it works. But there seems to be more to this that what I can find in neon-postgres documentation, node-postgres documentation or examples of implementation of neon-postgres with expressjs which is non-existant(?). Please help.
Edit: this is the error I receive:
PostgresError: invalid input syntax for type integer: “1,2,3”