如何修复绑定消息提供 4 个参数但准备语句“”需要 5 个

How do I fix bind message gives 4 parameters but prepared statement "" requires 5

我如何修复绑定消息给出 4 个参数,但准备语句“”需要 5 个 我尝试了与我有同样问题的人的解决方案,但是 none 是有帮助的。(我把所有的 类 放在一起,以便更容易理解错误) 我的 server.js

const express = require("express")
require('dotenv').config();
const productRoutes = require("./src/products/routes")

const app = express(); 
const port = 3004;

app.use(express.json())

app.get("/", (req,res)=>{
    res.send("Hello World!");
});

app.use("/api/v1/products", productRoutes)


app.listen(port, () => console.log(`app listening on port ${port}`))

我的db.js

const Pool = require('pg').Pool;
const pool = new Pool({
    user:process.env.POSTGRESQL_USERNAME,
    host:"localhost",
    database:"products",
    password:process.env.POSTGRESQL_PASSWORD,
    port: 5300,
});
module.exports = pool;

我的queries.js

const getProducts = "SELECT * FROM products";
const getProductsById = "SELECT * FROM products WHERE id=";
const checkImgExists = "SELECT * FROM products s WHERE s.img="
const addProduct = "INSERT INTO products (categoryid, title, unitsinstock, unitprice, img ) VALUES (, , , , )";

module.exports = {
    getProducts,
    getProductsById,
    checkImgExists,
    addProduct,
}

我的routes.js

const {Router}= require('express')
const controller = require('./controller')

const router= Router();

 router.get("/",  controller.getProducts)
 router.post("/", controller.addProduct)
router.get("/:id", controller.getProductsById)


 module.exports = router;

我的controller.js

const pool = require('../../db')
const queries = require("./queries")

const getProducts = (req,res)=>{
    pool.query(queries.getProducts,(error, results)=>{
        if(error) throw error;
        res.status(200).json(results.rows);
    })
}

const getProductsById = (req,res)=>{
    const id = parseInt(req.params.id);
    pool.query(queries.getProductsById,[id], (error, results)=>{
        if (error) throw error; 
        res.status(200).json(results.rows);
    })
}
 const addProduct=(req,res)=>{
     const {categoryid, title, unitsinstock, unitprice, img} = req.body;
     pool.query(queries.checkImgExists,[img], (error,results)=>{
         if(results.rows.length){
             res.send("same img")
         }

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

         );
     });
 }
module.exports = {
    getProducts,
    getProductsById,
    // getCategories,
    addProduct,
    
}

当我尝试使用邮递员添加数据时出现此错误:

app listening on port 3004
C:\Users530\Desktop\rest\src\products\controller.js:36
                 if(error) throw error;
                           ^

error:  bind message gives 4 parameters but prepared statement "" requires 5
:39:38)
    at Socket.<anonymous> (C:\Users530\Desktop\rest\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at Socket.Readable.push (internal/streams/readable.js:204:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  length: 202,
  severity: 'HATA',
  code: '08P01',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'd:\pginstaller_13.auto\postgres.windows-x64\src\backend\tcop\postgres.c',
  line: '1700',
  routine: 'exec_bind_message'
}

您在 addProduct 中忘记了一个变量:

[categoryid, title, unitsinstock, unitprice],

应该是

[categoryid, title, unitsinstock, unitprice, img]