PostgreSQL and JavaScript Template Tag Literal

PostgreSQL and JavaScript Template Tag Literal

If you need to create a search SQL statement for PostgreSQL in JavaScript using template literal, for example when using Vercel Postgres. You may have a SQL search statement like this:

`select c.id as id
  , c.word as word
	from cards c
	where word ilike '%${term}%'
	limit ${limit};`

Then you may run into this issue:

error: could not determine data type of parameter $1

The solution is to change the SQL to:

`select c.id as id
  , c.word as word
	from cards c
	where word ilike '%'||${term}||'%'
	limit ${limit};`

Then it will work. 😀

Developed with ❤️ by Aska