Postgres + Node - 带有 $1、$2、$3 的参数查询给出错误

Postgres + Node - Parameter Query with $1, $2, $3 giving error

当我尝试运行使用参数($1、$2、$3)查询时,我遇到了这个 nodejs 错误。该查询在 postgres 中有效。如果我不使用参数($1、$2、$3)而是用值代替它们,它也会起作用。谁能帮我找出问题所在?

   error: bind message supplies 3 parameters, but prepared statement "" requires 0 

我正在尝试的查询 运行:

var theQuery =   `DO
                $do$
                BEGIN
                IF EXISTS (SELECT * FROM commentsvoted WHERE id =  AND user_id = ) THEN
                    with t as (
                      SELECT votes FROM commentsvoted WHERE commentsvoted.id =  AND 
                 commentsvoted.user_id = 
                     ),
                     cv as (
                      UPDATE commentsvoted
                          SET votes = (CASE WHEN t.votes THEN NULL ELSE true END)
                          FROM t
                          WHERE commentsvoted.id =  AND commentsvoted.user_id = 
                     )
                      UPDATE comments
                      SET upvoted = (CASE WHEN t.votes THEN upvoted - 1
                                          WHEN t.votes IS NULL THEN upvoted + 1
                                          ELSE upvoted 
                                     END),
                          downvoted = (CASE WHEN NOT t.votes THEN downvoted - 1
                                            ELSE downvoted 
                                       END)

                      FROM t
                      WHERE comment_id =  AND posted_by = ;
                ELSE
                    with t as (UPDATE comments SET upvoted = upvoted + 1 
                    WHERE comment_id =  AND posted_by = )
                    INSERT INTO commentsvoted(id, user_id, votes, thread_id) 
                    VALUES(, , true, );
                END IF;
                END
                $do$`;


        client.query(theQuery, [commentId, userId, threadId])

Postgresql 不支持匿名块的参数

相关文档https://www.postgresql.org/docs/9.4/sql-do.html

相关工单https://github.com/brianc/node-postgres/issues/812

迁移到使用存储过程或函数似乎是您继续进行的最佳方式。该工单有使用 CREATE TEMPORARY FUNCTION

的建议