我正在尝试添加一个"post-question"函数,该函数向我的Postgres API/数据库发出post请求。
这是我的功能:
const postQuestion = (req, res) => {
let postVals = [req.params.id, Object.values(req.body)].flat();
let p_id = Number(req.params.id);
let q_body = postVals[1]
let q_date = postVals[2];
let name = postVals[3];
let help = postVals[4];
let reported = postVals[5];
pool.query(`SET search_path TO qa, public;
INSERT INTO questions(product_id, question_body, question_date, asker_name, helpfulness, reported)
VALUES ($1, $2, $3, $4, $5, $6)`, [p_id, q_body, q_date, name, help, reported], (err) => {
if (err) {
console.log(err)
}
res.send('success')
})
}
我正在用Postman测试它,这就是req.body的来源。
这是我的模式。。。。
CREATE TABLE questions (
question_id SERIAL PRIMARY KEY,
product_id INT,
question_body VARCHAR(255),
question_date VARCHAR(255),
asker_name VARCHAR(255),
helpfulness INT,
reported BOOLEAN
);
我得到了这个错误:
错误:无法在准备好的语句中插入多个命令在Parser.parseErrorMessage(/Users/megan/Bolder/SDC/ClarkFECSource/node_modules/pg-protocol/dist/Parser.js:278:15(
错误是由分号引起的,或者更确切地说,是由查询字符串包含两条语句这一事实引起的。
只需省略SET search_path
,将INSERT
修改为INSERT INTO qa.questions
。