我从控制器调用了以下函数:
async function insertUser(user_name, icon_link) {
await pool.query("DO $$BEGIN IF EXISTS (SELECT * FROM targets WHERE user_name = $1 ) THEN UPDATE targets SET icon_link = $2 , complaints = complaints + 1; ELSE INSERT INTO targets(user_name, icon_link, complaints) VALUES( $1 , $2 , 0); END IF; END; $$;", data);
}
当我尝试调用它时,收到以下错误:
const message = name === 'notice' ? new messages_1.NoticeMessage(length, messageValue) : new messages_1.DatabaseError(messageValue, length, name);
^
error: bind message supplies 2 parameters, but prepared statement "" requires 0
at Parser.parseErrorMessage (/Users/user/project/node_modules/pg-protocol/dist/parser.js:287:98)
at Parser.handlePacket (/Users/user/project/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/Users/user/project/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/Users/user/project/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:512:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
这是工作正常,直到它突然停止工作的一些原因,我不能理解。
我知道这个问题不是因为查询本身,因为同样的问题也存在于我的其他查询中。而且变量传递良好,我在调试时已经检查过了。
我试着像这样传递参数:
async function insertUser(user_name, icon_link) {
await pool.query({text: 'DO $$BEGIN IF EXISTS (SELECT * FROM targets WHERE user_name = $1 ) THEN UPDATE targets SET icon_link = $2 , complaints = complaints + 1; ELSE INSERT INTO targets(user_name, icon_link, complaints) VALUES( $1 , $2 , 0); END IF; END; $$;', values: [user_name, icon_link]});
}
但没有成功。
我还尝试通过在字符串参数中添加'来传递它们:
async function insertUser(user_name, icon_link) {
await pool.query("DO $$BEGIN IF EXISTS (SELECT * FROM targets WHERE user_name = '$1' ) THEN UPDATE targets SET icon_link = '$2' , complaints = complaints + 1; ELSE INSERT INTO targets(user_name, icon_link, complaints) VALUES( '$1' , '$2' , 0); END IF; END; $$;", data);
}
它也没有工作的原因很明显。
编辑:我还发现,如果不传递任何变量:
const res = await client.query('DO $$ BEGIN IF EXISTS (SELECT * FROM targets WHERE user_name = $1 ) THEN UPDATE targets SET icon_link = $2 , complaints = complaints + 1; ELSE INSERT INTO targets(user_name, icon_link, complaints) VALUES( $1 , $2 , 0); END IF; END $$;');
我得到:
error: there is no parameter $1
1条答案
按热度按时间njthzxwz1#
事先格式化查询可以解决这个问题,但我认为传递参数应该可以工作。您必须使用'而不是“才能工作。