我有一个数据库 goods
有两列 id jsonb primary_key
和 name
。使用此查询:
const query = 'INSERT INTO "goods" (id, name) VALUES ($1, $2)'
连同以下数据:
const data = {id: 1, name: "milk"};
给我以下错误:
{ [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' }
我设置了一个 postgres 数据库,通过 pg.Pool() 连接并执行 javascript 来插入我的数据。
编辑:这就是我准备查询的方式:
pool.query(query, [data]).then(() => {
console.log("ok")
})
.catch(error => {
console.log(error)
});
编辑2:
使用以下内容:
const query = 'INSERT INTO "goods" (id, name) VALUES ($1, $2)'
const data = JSON.stringify([1, "milk"]);
pool.query(query, data).then(() => {
console.log("ok")
})
.catch(error => {
console.log(error)
});
只是吐出以下错误: [TypeError: self.values.map is not a function]
原文由 optional 发布,翻译遵循 CC BY-SA 4.0 许可协议
根据 docs ,参数必须是 JavaScript 对象(即数组)。所以你不需要字符串化
data
尝试这个:
或者