【问题标题】:How do I fix bind message gives 4 parameters but prepared statement "" requires 5如何修复绑定消息给出 4 个参数但准备好的语句“”需要 5 个
【发布时间】:2021-12-21 18:38:14
【问题描述】:

如何修复绑定消息提供 4 个参数,但准备好的语句“”需要 5 个 我尝试了和我有同样问题的人的解决方案,但没有一个有帮助。(我把所有的类都放了,以便更容易理解错误) 我的 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;

我的查询.js

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

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

我的路线.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;

我的控制器.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:\Users\90530\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:\Users\90530\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'
}

【问题讨论】:

    标签: javascript node.js postgresql express postman


    【解决方案1】:

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

    [categoryid, title, unitsinstock, unitprice],
    

    应该是

    [categoryid, title, unitsinstock, unitprice, img]
    

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2014-11-30
      • 2013-08-09
      • 1970-01-01
      相关资源
      最近更新 更多