在连接中使用where子句的Knex错误

时间:2017-07-12 19:50:37

标签: mysql node.js knex.js

我使用以下查询:

const all = (context, merchantId) => {

  return knex(TABLE)
    .join('address', 'address.id', '=', 'hotel.id')
    .select(
        'hotel.id', 'hotel.name', 'hotel.description', 'hotel.thumbnail',
        'address.city', 'address.state', 'address.country')
    .where('merchant_id', merchantId)
    .then(rows => {
      return rows;
    });
};

但是得到错误:

Error: Undefined binding(s) detected when compiling SELECT query: select `hotel`.`id`, `hotel`.`name`, `hotel`.`description`, `hotel`.`thumbnail`, `address`.`city`, `address`.`state`, `address`.`country` from `hotel` inner join `address` on `address`.`id` = `hotel`.`id` where `merchant_id` = ?

我缺少什么?

1 个答案:

答案 0 :(得分:2)

根据knex.js documentation

  

重要:向任何地方提供未定义值的knex   函数将导致knex在sql编译期间抛出错误。   这既适合你,也适合我们。 Knex不知道该怎么做   where子句中的未定义值,通常它是a   编程错误提供一个开始。错误将抛出一个   包含查询类型和已编译查询字符串的消息。

     

示例:

knex('accounts')
.where('login', undefined)
.select()
.toSQL()
     

错误:

Undefined binding(s) detected when compiling SELECT query: select *
from `accounts` where `login` = ?

所以我猜你的代码中的merchantId是未定义的