【问题标题】:NodeJS PostgresSQL array of numbers treated as strings in WHERE IN clause在 WHERE IN 子句中被视为字符串的 NodeJS PostgreSQL 数字数组
【发布时间】: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


【解决方案1】:

既然你提到UNNEST 解决了这个问题。应该是req.body 不是数组的实例,而是一个字符串。这可能通过配置bodyParser 解决,或者您发送的请求可能有问题。

如果您不愿意为应用配置bodyParser,只需添加JSON.parse 即可解决您的问题:

pool.query(`SELECT count(*)
                  FROM ${dbConfig.schema}.orders
                  WHERE 1=1
                  AND order_number IN ($1)
                  AND id_user <> $2`
                , [JSON.parse(req.body), req.userId])
              .then(data => {
                res.status(200).send(data.rows)
                next()
              })
              .catch(error => {
                console.log(error)
                console.log(req.body)
              })

【讨论】:

    【解决方案2】:

    好的,UNNEST 解决了我的问题......把这个留给其他人

        pool.query(`SELECT count(*)
                      FROM ${dbConfig.schema}.customer_phone_map
                      WHERE 1=1
                      AND phone_number IN (SELECT UNNEST($1::int[]))
                      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)
                  })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2021-09-25
      • 2022-08-18
      • 2013-10-17
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多