You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复PostgreSQL报错:绑定消息提供4个参数但预编译语句需要5个

问题根因

报错的核心原因是执行INSERT语句时传入的参数数量和SQL定义的占位符数量不匹配:

  • queries.js中定义的addProduct插入语句包含5个占位符$1~$5,对应5个字段categoryid、title、unitsinstock、unitprice、img
  • controller.js的addProduct方法中调用插入SQL时,只传入了4个参数,遗漏了img参数
修复方案

修改controller.js中addProduct方法的插入语句参数数组,补上遗漏的img参数即可:

找到原有代码:

pool.query(
    queries.addProduct,
    [categoryid, title, unitsinstock, unitprice],
    (error, results)=>{
        if(error) throw error;
        res.status(201).send("success");
    }
);

替换为:

pool.query(
    queries.addProduct,
    [categoryid, title, unitsinstock, unitprice, img],
    (error, results)=>{
        if(error) throw error;
        res.status(201).send("success");
    }
);
可选优化建议
  • 当前代码校验图片重复后没有终止后续逻辑,可能会触发重复插入,建议补充return终止执行:
if(results.rows.length){
    return res.send("same img")
}
  • 建议新增请求参数校验逻辑,提前判断必填参数是否缺失,避免出现其他参数类报错。

内容的提问来源于stack exchange,提问作者Berke Özbek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 12:24:02