#javascript #node.js #postgresql #express #postman
Вопрос:
Как я могу исправить сообщение о привязке дает 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)=gt;{ res.send("Hello World!"); }); app.use("/api/v1/products", productRoutes) app.listen(port, () =gt; 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)=gt;{ pool.query(queries.getProducts,(error, results)=gt;{ if(error) throw error; res.status(200).json(results.rows); }) } const getProductsById = (req,res)=gt;{ const id = parseInt(req.params.id); pool.query(queries.getProductsById,[id], (error, results)=gt;{ if (error) throw error; res.status(200).json(results.rows); }) } const addProduct=(req,res)=gt;{ const {categoryid, title, unitsinstock, unitprice, img} = req.body; pool.query(queries.checkImgExists,[img], (error,results)=gt;{ if(results.rows.length){ res.send("same img") } //add pool.query( queries.addProduct, [categoryid, title, unitsinstock, unitprice], (error, results)=gt;{ if(error) throw error; res.status(201).send("success"); } ); }); } module.exports = { getProducts, getProductsById, // getCategories, addProduct, }
Когда я пытаюсь добавить данные с почтальоном, я получаю эту ошибку:
app listening on port 3004 C:Users90530Desktoprestsrcproductscontroller.js:36 if(error) throw error; ^ error: bind message gives 4 parameters but prepared statement "" requires 5 :39:38) at Socket.lt;anonymousgt; (C:Users90530Desktoprestnode_modulespg-protocoldistindex.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' }
Ответ №1:
вы забыли переменную здесь, в addProduct :
[categoryid, title, unitsinstock, unitprice],
должно быть
[categoryid, title, unitsinstock, unitprice, img]