【发布时间】:2021-07-28 11:08:34
【问题描述】:
我正在尝试使用 psql 中的 CONCAT 函数更新故事列,但不知何故不断收到错误消息,说它无法确定参数 $2 的数据类型。我哪里做错了?
router.put('/:id', (req,res) => {
db.query(`UPDATE stories SET story = CONCAT(story, $2) WHERE id = $1`,[req.params.id, 'text'])
.then(response => {
res.json({contribution:req.body.contribution_data, id:req.params.id});
})
.catch(err => console.log('Edit story error', err.message));
});
2021 年 5 月 6 日更新
感谢@Ayzrian 的建议
我按照建议执行了 PREPARE 语句,但现在出现了另一个错误:
“错误不能在准备好的语句中插入多个命令”
我做了与文档中建议的基本相同的格式:
router.put("/:id", (req, res) => {
db.query(
`
PREPARE testStory (int, text ) AS UPDATE stories SET story = CONCAT(story, $2) WHERE id = $1;
EXECUTE testStory ($1, $2);`,
[req.params.id, "text"]
)
更新解决方案
原来我可以通过在参数化值(在本例中为 $1)之后执行 ::text 来设置数据类型。这是解决方案代码:
db.query(
`
UPDATE stories
SET story = CONCAT(story, $1::text)
WHERE id = $2;
`,
["testinggg", req.params.id]
)
谢谢你的帮助!
【问题讨论】:
标签: node.js postgresql