postgresql 绑定消息提供 1 个参数,但准备好的语句 "" 需要 2 个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43755454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:33:42  来源:igfitidea点击:

Bind message supplies 1 parameters, but prepared statement "" requires 2

node.jspostgresql

提问by optional

I have a database goodswith two columns id jsonb primary_keyand name. Using this query:

我有一个goods包含两列id jsonb primary_keyname. 使用此查询:

const query = 'INSERT INTO "goods" (id, name) VALUES (, )'

together with the following data:

连同以下数据:

const data = {id: 1, name: "milk"};

gives me the following error:

给了我以下错误:

{ [error: bind message supplies 1 parameters, but prepared statement "" requires 2]
  name: 'error',
  length: 130,
  severity: 'ERROR',
  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: 'postgres.c',
  line: '1556',
  routine: 'exec_bind_message' }

I have a postgres database set up, connected via pg.Pool() and executing javascript to insert my data.

我设置了一个 postgres 数据库,通过 pg.Pool() 连接并执行 javascript 来插入我的数据。

Edit: This is how I prepare my query:

编辑:这就是我准备查询的方式:

pool.query(query, [data]).then(() => {
    console.log("ok")
})
.catch(error => {
    console.log(error)
});

Edit2:

编辑2:

Using the following:

使用以下内容:

const query = 'INSERT INTO "goods" (id, name) VALUES (, )'
const data = JSON.stringify([1, "milk"]);
pool.query(query, data).then(() => {
    console.log("ok")
})
.catch(error => {
    console.log(error)
});

Just spits out the following error: [TypeError: self.values.map is not a function]

只是吐出以下错误: [TypeError: self.values.map is not a function]

采纳答案by Priyesh Kumar

As per docs, parameters must be JavaScript object (which is array). So you don't need to stringify data

根据docs,参数必须是 JavaScript 对象(即数组)。所以你不需要字符串化data

Try this:

试试这个:

const query = 'INSERT INTO goods (id, name) VALUES (, )'

const data = [1, "milk"];

pool.query(query, data).then(....)

Or

或者

pool.query({ 
    text: 'INSERT INTO goods (id, name) VALUES (, )', 
    values: [1, 'milk']
 }).then(...)

回答by vitaly-t

As per documentation, a Prepared Statement expects an array of values, not an object with properties, i.e. your data must be: const data = [1, "milk"];

根据文档,准备好的语句需要一个值数组,而不是具有属性的对象,即您的数据必须是:const data = [1, "milk"];