Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加knex.js.andWhere()为条件为真_Javascript_Node.js_Knex.js - Fatal编程技术网

Javascript 添加knex.js.andWhere()为条件为真

Javascript 添加knex.js.andWhere()为条件为真,javascript,node.js,knex.js,Javascript,Node.js,Knex.js,我有一个疑问: const customerProducts = db.sequelize.knex.select([ 'CustomerProduct.id AS _id', 'CustomerProduct.last_delivered AS _lastDelivered', 'CustomerProduct.margin AS _margin', 'CustomerProduct.outlier AS _outlier', 'Cu

我有一个疑问:

const customerProducts = db.sequelize.knex.select([
      'CustomerProduct.id AS _id',
      'CustomerProduct.last_delivered AS _lastDelivered',
      'CustomerProduct.margin AS _margin',
      'CustomerProduct.outlier AS _outlier',
      'CustomerProduct.growth AS _growth',
      'CustomerProduct.period AS _period',
      'CustomerProduct.price AS _price',
      'CustomerProduct.active AS _active',
      'CustomerProduct.customer_id AS _customerId',
      'CustomerProduct.product_id AS _productId',
      'CustomerProduct.modified AS _modified',
      'CustomerProduct.month_value AS _monthValue',
      'customer.id AS _customer_id',
      'customer.title AS _customer_title',
      'customer.code AS _customer_code',
    ])
      .from('customer_products AS CustomerProduct')
      .innerJoin(
        'customers AS customer',
        'CustomerProduct.customer_id',
        'customer.id',
      )
      .where(whereClause)
      .limit(limit)
      .offset(offset);
我想包括一个
.andWhere()
选项,该选项仅在满足此条件时才会添加到查询中:

if (overdue === 'true')

我该怎么做?

您可以这样做:

const query = db.sequelize.knex.select([
      'CustomerProduct.id AS _id',
      'CustomerProduct.last_delivered AS _lastDelivered',
      'CustomerProduct.margin AS _margin',
      'CustomerProduct.outlier AS _outlier',
      'CustomerProduct.growth AS _growth',
      'CustomerProduct.period AS _period',
      'CustomerProduct.price AS _price',
      'CustomerProduct.active AS _active',
      'CustomerProduct.customer_id AS _customerId',
      'CustomerProduct.product_id AS _productId',
      'CustomerProduct.modified AS _modified',
      'CustomerProduct.month_value AS _monthValue',
      'customer.id AS _customer_id',
      'customer.title AS _customer_title',
      'customer.code AS _customer_code',
    ])
      .from('customer_products AS CustomerProduct')
      .innerJoin(
        'customers AS customer',
        'CustomerProduct.customer_id',
        'customer.id',
      );
if (overdue === 'true') {
  query = query.where(whereClause)
}
const customerProducts = query.limit(limit).offset(offset);

实际上我已经厌倦了,但是得到了
uncaughtException:await仅在异步函数中有效。你知道为什么吗?@runnerpaul你在一个未声明为
async
的函数中使用
await
(例如:
async function something(){….}
)。