Home > Software engineering >  How do I fix bind message gives 4 parameters but prepared statement "" requires 5
How do I fix bind message gives 4 parameters but prepared statement "" requires 5

Time:11-09

How i can fix bind message gives 4 parameters but prepared statement "" requires 5 I tried the solutions of people who had the same problem as me, but none of them were helpful.(I put all the classes so that the error can be understood more easily) My 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}`))

My 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;

My queries.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,
}

My 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;

My 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,
    
}

When I try to add data with postman I get this error:

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'
}

CodePudding user response:

you forgot a variable here in addProduct :

[categoryid, title, unitsinstock, unitprice],

should be

[categoryid, title, unitsinstock, unitprice, img]
  • Related