Node.js 为什么我的sql多行插入查询包含语法错误?

Node.js 为什么我的sql多行插入查询包含语法错误?,node.js,postgresql,Node.js,Postgresql,我正在heroku上用nodejs处理post请求,并试图将json插入postgres数据库 const client = await pool.connect(); var jsondata = req.body; var values = []; for(var i=0; i< jsondata.length; i++) { values.push([jsondata[i].id,jsondata[i].tit

我正在heroku上用nodejs处理post请求,并试图将json插入postgres数据库

const client = await pool.connect();
    var jsondata = req.body;
    var values = [];
    for(var i=0; i< jsondata.length; i++) {
                         values.push([jsondata[i].id,jsondata[i].title,jsondata[i].imageUrl,jsondata[i].orderPosition]);
                    }
const sqlQuery = 'INSERT INTO showme_gardens_tourMapTableTest (mapId, imageId, title, position) VALUES ?;';
console.log("$%$% " + values);                  
const  resultOfMapsInsert = await client.query(sqlQuery, [values]);
const client=wait pool.connect();
var jsondata=请求主体;
var值=[];
对于(var i=0;i
我得到一个语法错误

2019-10-15T13:08:35.238210+00:00 app[web.1]: $%$% 25,Wind in the willows,http://something.com,4
2019-10-15T13:08:35.244196+00:00 app[web.1]: { error: syntax error at or near "?"
2019-10-15T13:08:35.244199+00:00 app[web.1]: at Connection.parseE (/app/node_modules/pg/lib/connection.js:602:11)
2019-10-15T13:08:35.244202+00:00 app[web.1]: at Connection.parseMessage (/app/node_modules/pg/lib/connection.js:399:19)
2019-10-15T13:08:35.244204+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:121:22)
2019-10-15T13:08:35.244206+00:00 app[web.1]: at Socket.emit (events.js:198:13)
2019-10-15T13:08:35.244208+00:00 app[web.1]: at addChunk (_stream_readable.js:288:12)
2019-10-15T13:08:35.244210+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:269:11)
2019-10-15T13:08:35.244212+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:224:10)
2019-10-15T13:08:35.244214+00:00 app[web.1]: at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
2019-10-15T13:08:35.244216+00:00 app[web.1]: name: 'error',
2019-10-15T13:08:35.244218+00:00 app[web.1]: length: 90,
2019-10-15T13:08:35.244220+00:00 app[web.1]: severity: 'ERROR',
2019-10-15T13:08:35.244222+00:00 app[web.1]: code: '42601',
2019-10-15T13:08:35.238210+00:00应用程序[web.1]:$%%%25,柳树中的风,http://something.com4.
2019-10-15T13:08:35.244196+00:00应用程序[web.1]:{错误:语法错误在“”处或附近
2019-10-15T13:08:35.244199+00:00应用程序[web.1]:在Connection.parseE(/app/node_modules/pg/lib/Connection.js:602:11)
2019-10-15T13:08:35.244202+00:00应用程序[web.1]:at Connection.parseMessage(/app/node_modules/pg/lib/Connection.js:399:19)
2019-10-15T13:08:35.244204+00:00应用程序[web.1]:at套接字。(/app/node_modules/pg/lib/connection.js:121:22)
2019-10-15T13:08:35.244206+00:00应用程序[web.1]:位于Socket.emit(events.js:198:13)
2019-10-15T13:08:35.244208+00:00应用程序[web.1]:位于addChunk(_stream_readable.js:288:12)
2019-10-15T13:08:35.244210+00:00应用程序[web.1]:位于readableAddChunk(_stream_readable.js:269:11)
2019-10-15T13:08:35.244212+00:00应用程序[web.1]:位于Socket.Readable.push(_stream_Readable.js:224:10)
2019-10-15T13:08:35.244214+00:00应用程序[web.1]:位于TCP.onStreamRead[as-onread](内部/流基础共享文件:94:17)
2019-10-15T13:08:35.244216+00:00 app[web.1]:名称:“错误”,
2019-10-15T13:08:35.244218+00:00应用程序[网站1]:长度:90,
2019-10-15T13:08:35.244220+00:00应用程序[web.1]:严重性:“错误”,
2019-10-15T13:08:35.244222+00:00应用程序[网站1]:代码:“42601”,

如果您使用的是pg模块,则可以按照以下方式构造查询:

const query = {
  text: 'INSERT INTO tableName (field1, field2) VALUES($1, $2)',
  values: [value1, value2],
}

let result = await client.query(query);
node pg
官方文件说:

参数化查询

如果要将参数传递给查询,则需要避免将参数直接串接到查询文本中导致SQL注入漏洞。node postgres支持参数化查询,将未经更改的查询文本和参数传递给PostgreSQL server,在该服务器上,参数将安全地替换到查询中,并在服务器本身内使用经过战斗测试的参数替换代码

对于多行,我建议使用
pg promise

const pgp = require('pg-promise')(/* initialization options */);

// Database connection details;
const cn = {
    host: 'localhost', // 'localhost' is the default;
    port: 5432, // 5432 is the default;
    database: 'myDatabase',
    user: 'myUser',
    password: 'myPassword'
};
const db = pgp(cn);

const colset = new pgp.helpers.ColumnSet(['col1', 'col2'], {table: 'tablename'});

// data input values:
const values = [{col1: value1, col2:value2}, {col1: value3, col2: value4}];

// generating a multi-row insert query:
const query = pgp.helpers.insert(values, colset);
//=> INSERT INTO "tablename"("col1","col2") VALUES(value1,value2),(value3,value4)

await db.none(query); // executing the query
你确定“?”就在这里吗?我不知道pg模块,但它在postgres中肯定无效。这个问题似乎暗示了同样的问题,快速查看node的pg文档可以发现它使用了常规的
$1
语法。同时检查一下这个