gpt4 book ai didi

javascript - 数组未传递给 knex 中的查询

转载 作者:数据小太阳 更新时间:2023-10-29 05:11:49 25 4
gpt4 key购买 nike

我正在将 id 数组从 get 查询传递到 knex whereIn 函数,但它们将丢失。

if(query.cols){
var cols = query.cols.map(Number);
console.log(cols)
search.whereIn('collection_id', cols)
}

我正在将它们映射到查询的整数。控制台日志是...

[ 77, 66 ]

但调试显示查询为...

...and "collection_id" in (?, ?) 

我错过了什么?

最佳答案

值显示为字符串,因为 knex 要求将数组作为包含数组中的参数传递。来自 raw bindings 的文档:

Note that due to ambiguity, arrays must be passed as arguments within a containing array.

knex.raw('select * from users where id in (?)', [1, 2, 3]);
// Error: Expected 3 bindings, saw 1

knex.raw('select * from users where id in (?)', [[1, 2, 3]])
Outputs:
select * from users where id in (1, 2, 3)

您可以通过在数组本身内传递 cols 数组来解决此问题:

if (query.cols) {
var cols = query.cols.map(Number);
console.log(cols)
search.whereIn('collection_id', [cols])
}

关于javascript - 数组未传递给 knex 中的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com