For debugging, you might try to sending each select, update, and insert in separate NodeJS query statements. You can do the IF work in NodeJS via a select for data from Pg.
But at the core of your problem I believe is that you are not configuring the postgres statement to receive parameters, but your injecting parameters from Node. From the Pg docs on prepared statements: https://www.postgresql.org/docs/current/sql-prepare.html
PREPARE fooplan (int, text, bool, numeric) AS INSERT INTO foo VALUES($1, $2, $3, $4); EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);