数组未传递给 knex 中的查询
Array not being passed to query in knex
我正在将 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])
}
根据关于原始参数绑定的 Knex 文档,我们需要为数组中将绑定到查询的每个元素添加 ?
:
Since there is no unified syntax for array bindings, instead you need to treat them as multiple values by adding ? directly in your query.
http://knexjs.org/#Raw-Bindings
const myArray = [1,2,3]
knex.raw('select * from users where id in (' + myArray.map(_ => '?').join(',') + ')', [...myArray]);
// query will become: select * from users where id in (?, ?, ?) with bindings [1,2,3]
所有感谢@chiragsrvstv
而不是 knex.row 我使用 whereRaw 这里是代码
const whereClause= [ 75, 76 ]
//if array length is 0 then have this
//`select * from "users" where id!=0`
if(whereClause.length <= 0) {
whereClause= [ 0 ]
}
console.log( knex('users').whereRaw("id!="+whereClause.map(_ => '?').join(' and id!='), [...whereClause]).toString())
我正在将 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])
}
根据关于原始参数绑定的 Knex 文档,我们需要为数组中将绑定到查询的每个元素添加 ?
:
Since there is no unified syntax for array bindings, instead you need to treat them as multiple values by adding ? directly in your query. http://knexjs.org/#Raw-Bindings
const myArray = [1,2,3]
knex.raw('select * from users where id in (' + myArray.map(_ => '?').join(',') + ')', [...myArray]);
// query will become: select * from users where id in (?, ?, ?) with bindings [1,2,3]
所有感谢@chiragsrvstv
而不是 knex.row 我使用 whereRaw 这里是代码
const whereClause= [ 75, 76 ]
//if array length is 0 then have this
//`select * from "users" where id!=0`
if(whereClause.length <= 0) {
whereClause= [ 0 ]
}
console.log( knex('users').whereRaw("id!="+whereClause.map(_ => '?').join(' and id!='), [...whereClause]).toString())