我如何修复绑定消息给出了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;我的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,
}我的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,
}当我尝试用postman添加数据时,我得到了这个错误:
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'
}发布于 2021-11-09 01:19:39
您在addProduct中忘记了一个变量:
[categoryid, title, unitsinstock, unitprice],应该是
[categoryid, title, unitsinstock, unitprice, img]https://stackoverflow.com/questions/69891664
复制相似问题