Javascript: Postgres querying jsonb array throws could not determine data type of parameter $1

Viewed 23

I have a a table account with the following columns:

id     | integer                
username   | character varying(30) 
data | jsonb

In data column, I am saving an array of user addresses along with the location. The sql statement to select location from the table works as raw query but trying it with the prepared format that pg or any other orm offers is not working for me.

The query format is: pg.query("SELECT * FROM account where data->'addresses' @> '[{"location":"$1"}]'", ['Berlin']).

It throws the following error:

could not determine data type of parameter $1

I know that raw query could also be used instead but I am considering the security as well here

1 Answers

You can't use $1 to interpolate into the middle of an SQL string. Build the entire JSON in the client and pass it as a unit to the query.

Related