传递参数Node.js时调用了错误的命名查询

传递参数Node.js时调用了错误的命名查询,node.js,pg-promise,Node.js,Pg Promise,我刚从Node.js、postgresql和pg promise开始,在我的API的get端点中,我检查存在哪些参数,因此在cascade中,如果存在城市、地区、国家,我调用'get-city-products'查询,如果存在地区、国家,我调用'get-region-products',如果country存在,我会调用“get-country-products”。 当我只通过地区、国家/地区或国家/地区时,它调用'get-city-products',它没有城市或城市、地区失败,并且不返回任何产

我刚从Node.js、postgresql和pg promise开始,在我的API的get端点中,我检查存在哪些参数,因此在cascade中,如果存在
城市、地区、国家,我调用
'get-city-products'
查询,如果存在
地区、国家,我调用
'get-region-products'
,如果
country
存在,我会调用
“get-country-products”
。 当我只通过
地区、国家/地区或
国家/地区时,它调用
'get-city-products'
,它没有
城市或
城市、地区
失败,并且不返回任何产品。。 当缺少
城市
参数时,为什么
else if(城市、地区、国家)
成功?你能发现我的业务逻辑有什么问题吗? 以下是固有的代码:

// city
  else if (city, region, country) {
    await db.any({
      name: 'get-city-products',
      text: 'SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3',
      values: [city, region, country]
    })
      .then(result => {
        console.log('get-city-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-city-products error:', error);
      });
  }

  // region => never gets called
  else if (region, country) {
    await db.any({
      name: 'get-region-products',
      text: 'SELECT * FROM products WHERE region = $1 AND country = $2',
      values: [region, country]
    })
      .then(result => {
        console.log('get-region-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-region-products error:', error);
      });
  }

  // country => never gets called
  else if (country) {
    await db.any({
      name: 'get-country-products',
      text: 'SELECT * FROM products WHERE country = $1',
      values: [country]
    })
      .then(result => {
        console.log('get-country-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-country-products error:', error);
      });
  }
下面是完整的方法:

exports.findProducts = async (req, res) => {

  // customers queries

  // sorted
  if (city, region, country, category, minPrice, maxPrice, orderedBy, sorted) {

    /// this will create a unique PreparedStatement name for each query type
    const name = 'getCityCategoryPriceRangeOrderedBy' + orderedBy + '' + sorted + ' Products';

    const text = pgp.as.format(`SELECT * FROM products WHERE city = $/city/
                            AND region = $/region/ AND country = $/country/
                            AND category = $/category/
                            AND price BETWEEN $/minPrice/ AND $/maxPrice/
                            ORDER BY $/orderedBy:name/ $/sorted:value/`, {
      city, region, country, category,
      minPrice, maxPrice, orderedBy, sorted
    });

    console.log('query for ' + name + ' is: ', text,);

    await db.any({ name: name, text: text })
      .then(result => {
        console.log(name + ' results: ', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log(name + ` error:`, error);
      });

  }


  // price range
  else if (city, region, country, category, minPrice, maxPrice) {
    await db.any({
      name: 'get-city-category-price-range-products',
      text: 'SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3 AND category = $4 AND price BETWEEN $5 AND $6',
      values: [city, region, country, category, minPrice, maxPrice]
    })
      .then(result => {
        console.log('get-city-category-price-range-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-city-category-price-range-products error:', error);
      });
  }

  // city category
  else if (city, region, country, category) {
    await db.any({
      name: 'get-city-category-products',
      text: 'SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3 AND category = $4',
      values: [city, region, country, category]
    })
      .then(result => {
        console.log('get-city-category-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-city-category-products error:', error);
      });
  }


  // city
  else if (city, region, country) {
    await db.any({
      name: 'get-city-products',
      text: 'SELECT * FROM products WHERE city = $1 AND region = $2 AND country = $3',
      values: [city, region, country]
    })
      .then(result => {
        console.log('get-city-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-city-products error:', error);
      });
  }

  // region => never gets called
  else if (region, country) {
    await db.any({
      name: 'get-region-products',
      text: 'SELECT * FROM products WHERE region = $1 AND country = $2',
      values: [region, country]
    })
      .then(result => {
        console.log('get-region-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-region-products error:', error);
      });
  }

  // country => never gets called
  else if (country) {
    await db.any({
      name: 'get-country-products',
      text: 'SELECT * FROM products WHERE country = $1',
      values: [country]
    })
      .then(result => {
        console.log('get-country-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-country-products error:', error);
      });
  }


// shops queries

  else if (vendor) {
    await db.any({
      name: 'get-vendor-products',
      text: 'SELECT * FROM products WHERE vendor = $1',
      values: [vendor]
    })
      .then(result => {
        console.log('get-vendor-products:', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found.'
          });
        }
      })
      .catch(function (error) {
        console.log('get-vendor-products error:', error);
      });
  }



  else {
    db.any({
      name: 'get-all-products',
      text: 'SELECT * FROM products ORDER BY id ASC',
    })
      .then(result => {
        console.log('get-all-products', result);
        if (result.length > 0) {
          res.status(200).send({
            data: result
          });
        }
        else {
          res.status(404).send({
            error: 'No product found'
          });
        }
      })
      .catch(function (error) {
        console.log('get-cityProducts error:', error);
      });
  }
};

我应该在每个查询中使用一个参数吗?

在条件子句(else-ifs)中,用逗号分隔不同的值

else if (city, region, country) {
虽然不是完全无效的语法(只使用最后一个变量的值),但您可能正在寻找布尔比较

else if (city && region && country)

如果所有的变量都是真的,那么条件就是执行。

你完全正确,我自己也看不到,这就是为什么
排序的
赖斯范围的
查询确实有效。。他们只检查元组中的最后一个参数..谢谢,我在兜圈子哈哈。干杯。