数组未被传递到knex中查询
我从一个get查询传递一个ID数组在哪里函数,但他们将失踪。
if(query.cols){ var cols = query.cols.map(Number); console.log(cols) search.whereIn('collection_id', cols) }
我将它们映射到整数的查询。 控制台日志是…
[ 77, 66 ]
但debugging显示查询为…
...and "collection_id" in (?, ?)
我错过了什么?
值显示为string,因为knex
要求将数组作为parameter passing给包含数组。 从原始绑定的文档:
请注意,由于歧义,数组必须在包含数组中作为parameter passing。
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]) }