Javascript 仅当名称和位置不';不匹配
我只尝试在psql表中添加一条记录,如果它具有唯一的名称和位置。如果条目在表(名称或位置)中不存在,则可以插入记录,但如果名称已经存在,则我的服务器会在响应查询时抛出错误。这是我目前的代码:Javascript 仅当名称和位置不';不匹配,javascript,sql,postgresql,postman,psql,Javascript,Sql,Postgresql,Postman,Psql,我只尝试在psql表中添加一条记录,如果它具有唯一的名称和位置。如果条目在表(名称或位置)中不存在,则可以插入记录,但如果名称已经存在,则我的服务器会在响应查询时抛出错误。这是我目前的代码: app.post("/addCampground", async (req, res) => { const name = req.body.name; const location = req.body.location; const leng = re
app.post("/addCampground", async (req, res) => {
const name = req.body.name;
const location = req.body.location;
const leng = req.body.maxlength;
const elev = req.body.elevation;
const site = req.body.sites;
const pad = req.body.pad;
try{
const template = "INSERT INTO campgrounds (name, location, maxlength, elevation,
sites, padtype) VALUES ($1, $2, $3, $4, $5, $6) SELECT name, location WHERE NOT
EXISTS(SELECT name, location from campgrounds where name =$1 AND location =$2) ";
console.log(template);
const response = await pool.query(template, [name, location, leng, elev, site,
pad], [name, location]);
res.json({status: "added", results:{name:name, location:location} });
}catch (err){
res.json({status: "campground already in database"});
}
})
问题是:
const template = "INSERT INTO campgrounds (name, location, maxlength, elevation, sites, padtype) VALUES
($1, $2, $3, $4, $5, $6) SELECT name, location WHERE NOT EXISTS(SELECT name, location from campgrounds
where name =$1 AND location =$2) ";
const response = await pool.query(template, [name, location, leng, elev, site, pad], [name, location]);
尝试添加具有匹配名称但位置不同的记录时出错:
TypeError: cb is not a function
at Query.callback (/home/sbeg/db-class-350/practice/task4/node_modules/pg-pool/index.js:376:18)
at Query.handleError (/home/sbeg/db-class-350/practice/task4/node_modules/pg/lib/query.js:128:19)
at Client._handleErrorMessage (/home/sbeg/db-class-350/practice/task4/node_modules/pg/lib/client.js:335:17)
at Connection.emit (events.js:315:20)
at /home/sbeg/db-class-350/practice/task4/node_modules/pg/lib/connection.js:115:12
at Parser.parse (/home/sbeg/db-class-350/practice/task4/node_modules/pg-protocol/dist/parser.js:40:17)
at Socket.<anonymous> (/home/sbeg/db-class-350/practice/task4/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
TypeError:cb不是函数
在Query.callback(/home/sbeg/db-class-350/practice/task4/node_modules/pg pool/index.js:376:18)
在Query.handleError(/home/sbeg/db-class-350/practice/task4/node_modules/pg/lib/Query.js:128:19)
在客户端处理错误消息(/home/sbeg/db-class-350/practice/task4/node_modules/pg/lib/Client.js:335:17)
在Connection.emit(events.js:315:20)
at/home/sbeg/db-class-350/practice/task4/node_modules/pg/lib/connection.js:115:12
在Parser.parse(/home/sbeg/db-class-350/practice/task4/node_modules/pg protocol/dist/Parser.js:40:17)
在插座上。(/home/sbeg/db-class-350/practice/task4/node_modules/pg protocol/dist/index.js:10:42)
在Socket.emit(events.js:315:20)
在addChunk(_stream_readable.js:295:12)
在readableAddChunk(_stream_readable.js:271:9)
sql的正确语法:
INSERT INTO campgrounds (name, location, maxlength, elevation, sites, padtype)
SELECT ($1, $2, $3, $4, $5, $6)
WHERE NOT EXISTS(SELECT 1 from campgrounds where name =$1 AND location =$2)
或者,如果名称、位置列上有唯一索引,则可以在Conflict上使用:
INSERT INTO campgrounds (name, location, maxlength, elevation, sites, padtype)
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT unique_index DO NOTHING
在insert语句中不能同时使用值和SELECT。看起来你应该使用插入。。。冲突。是否类似于“在冲突中插入营地(名称、位置、最大长度、标高、场地、焊盘类型)值($1、$2、$3、$4、$5、$6)不执行任何操作”?如果我将其更改为此,则不会插入任何内容