我们正在使用KNEX中的一系列列名:
knex
.select( ["rowid", "accountid", "accountname"] )
.from("account")
.then(function (e, rows) {
callback(e, rows)
})
并获得以下错误:
select `rowid,accountid,accountname` from `account`
Unhandled rejection Error: ER_BAD_FIELD_ERROR: Unknown column 'rowid,accountid,accountname' in 'field list'
显然,列名数组已转换为导致错误的字段字符串。使用单个字段正常工作:
knex
.select( "rowid", "accountid", "accountname" )
.from('account')
.then(function (e, rows) {
callback(e, rows)
})
这是一个已知问题吗?是否有使用"选择"函数的数组的方法?
我使用KNEX 0.9.0进行了测试,并且它接受了select
的数组参数,而无需抱怨:
knex.
select(['c1','t1','c2']).
from('sample').
then(function(rows) {
console.dir(rows);
});
// console.dir output:
// [ { c1: 22, t1: 'xx', c2: 33 },
// { c1: 422, t1: 'xxxx', c2: 77 } ]
您可能已经注意到您的示例无论如何都有问题,then()
不遵循常见回调错误的第一样式(callback(error, result)
),但相反:then(success_callback, failure_callback)
,bluebird提供便利的方法catch()
以避免故障呼叫。因此,您可以将代码更改为:
knex
.select( ["rowid", "accountid", "accountname"] )
.from('account')
.then(function (rows) {
callback(null, rows)
})
.catch(function (e) {
callback(e, null);
});
如果您仍然碰巧使用的是不允许列名称数组的KNEX,则可以通过.apply()
调用替换直接调用,例如:
knex
.select.apply(knex, ["rowid", "accountid", "accountname"] )
.from('account')
.then(function (rows) {
callback(null, rows)
})
.catch(function (e) {
callback(e, null);
});