gpt4 book ai didi

node.js - Nodejs - Postgres - 通知 - 错误绑定(bind)消息

转载 作者:行者123 更新时间:2023-11-29 12:53:02 26 4
gpt4 key购买 nike

我正在使用 node-postgres (v-6.1.2)

这是我正在重构的代码,因为查询中有未参数化的参数。这不是我写的代码!

我有以下函数 publish,其中 pubClient

的结果
new pg.Client(pubsubUri).connect((err, client) => {
if(err) return logger.error('database-connect', err.message)

pubClient = client
})

当我运行 publish 时,出现以下错误:

错误:绑定(bind)消息提供 2 个参数,但准备语句“”需要 0 个

这是我的发布方法:(类型和消息都是字符串类型)

let publish = ( type, message) => {
if (pubClient) {
pubClient.query('NOTIFY "$1", \'$2\'', [type, message], (err, res) => {
if (err) {
console.log(err)
return
}
return true
})

}

return false
}

我不知道查询的正确语法是什么,也不知道为什么会出现此错误。

如有任何帮助,我们将不胜感激!

最佳答案

NOTIFY channel [ , payload ]

channel 不能参数化。尝试使用 pg_notify:

let publish = (type, message) => {
if (pubClient) {
pubClient.query('SELECT pg_notify($1, $2)', [type, message], (err, res) => {
if (err) {
console.log(err)
return
}
return true
})

}

return false
}

关于node.js - Nodejs - Postgres - 通知 - 错误绑定(bind)消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49655799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com