Using Transaction with if-else in prepared statement - Mailing list pgsql-sql

From Hui Jackson
Subject Using Transaction with if-else in prepared statement
Date
Msg-id CAHXAyjx-2MhO0zoCt3Vr_2AW6sFVjN0tgDo_GPVg=DVCzQDQMQ@mail.gmail.com
Whole thread Raw
Responses Re: Using Transaction with if-else in prepared statement
Re: Using Transaction with if-else in prepared statement
List pgsql-sql
I am trying to make transaction in nodejs
The logic will be check if sufficient coin in sender's amount, if sufficient then do transaction.
I am new to postgres, not sure if this is a right way to do so, if you have a better solution, please let me know, thank you.

const coin = 10
const senderId = 1
const receiverId = 2
await pgPool.query(`
DO $$
    BEGIN
        if (SELECT coin from app_user where id = $1) >= $3 THEN
            UPDATE app_user set coin = coin - $3 where id = $1;
            UPDATE app_user set coin = coin + $3 where id = $2;
            INSERT INTO coin_history(sender_id, receiver_id, amount) VALUES ($1, $2, $3)

        END IF;
    END;
$$`,[senderId, receiverId, coin]);

Error (node:10044) UnhandledPromiseRejectionWarning: error: bind message supplies 3 parameters, but prepared statement "" requires 0

pgsql-sql by date:

Previous
From: Frank Streitzig
Date:
Subject: Re: Count child objects for each line of a table: LEFT JOIN, LATERAL JOIN or subqueries ?
Next
From: Steve Midgley
Date:
Subject: Re: Using Transaction with if-else in prepared statement