【发布时间】:2021-01-04 00:01:02
【问题描述】:
我想在 where 查询中包含 IN 子句,但无论出于何种原因,NodeJS 都将其视为字符串数组。
pool.query(`SELECT count(*)
FROM ${dbConfig.schema}.orders
WHERE 1=1
AND order_number IN ($1)
AND id_user <> $2`
, [req.body, req.userId])
.then(data => {
res.status(200).send(data.rows)
next()
})
.catch(error => {
console.log(error)
console.log(req.body)
})
输出:
error: invalid input syntax for type bigint: "{"20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40"}"
at Parser.parseErrorMessage ...
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:302:12)
at readableAddChunk (_stream_readable.js:278:9)
at Socket.Readable.push (_stream_readable.js:217:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
length: 197,
severity: 'ERROR',
code: '22P02',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'int8.c',
line: '126',
routine: 'scanint8'
}
[
20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40
]
如您所见,它说:
invalid input syntax for type bigint: "{"20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40"}"
但我正在发送:
[ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ]
知道发生了什么吗?我错过了什么吗?
【问题讨论】:
-
我无法帮助您解决库错误发送的参数,但您的查询有错误。
order_number IN ($1)应该是order_number = ANY ($1)以使其与数组一起使用。
标签: node.js postgresql