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

Knex error using where clause in joins

我正在使用以下查询:

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` = ?

我错过了什么?

根据knex.js documentation

Important: Supplying knex with an undefined value to any of the where functions will cause knex to throw an error during sql compilation. This is both for yours and our sake. Knex cannot know what to do with undefined values in a where clause, and generally it would be a programmatic error to supply one to begin with. The error will throw a message containing the type of query and the compiled query-string.

Example:

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

Error:

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

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