【问题标题】:Nonanchored Substring Query a field with Loopback APINonanchored Substring 使用 Loopback API 查询字段
【发布时间】:2016-12-08 05:24:36
【问题描述】:

我正在尝试使用 loopback api 为 typeahead 字段实现一个简单的子字符串查询,也许我是盲人,但我一直在寻找这个问题的答案。

我只想为其提供一个子字符串,并让它返回名称中包含该子字符串的所有品牌。

例如这个请求/api/brands/search?q=tas会返回。

[{
  "brandName": "TastyKakes",
  "id": "4"
},
{
  "brandName": "FantasticPastries",
  "id": "10"
}]

我写了一个 remoteMethod 来处理这个问题,但我不能让它返回我正在寻找的数据

  Brand.search = function (q, callback) {
    var pattern = new RegExp(q, "i");
    var brands = Brand.find({
      where: {
        brandName: {
          like: pattern
        }
      }
    }, function (err, data) {
      callback(err, data)
    })
  };

我是 Loopback / Node / Express 的新手,但我来自 Python / Django 背景,在 django 中做这样的事情非常简单。我觉得我一定是错过了什么。

【问题讨论】:

    标签: javascript node.js express loopbackjs


    【解决方案1】:

    我想这可以通过 ilike %% no case in where 子句这样不需要模式来完成

    Brand.search = function (q, callback) {
    var pattern = "%s" + q + "%s";
    var brands = Brand.find({
      where: {
        brandName: {
          ilike: pattern
        }
      }
    }, function (err, data) {
      callback(err, data)
    })
    };
    

    更多详情见HERE

    【讨论】:

    • 我应该澄清我正在使用 mysql,但你的回答让我走上了正确的道路。实际上有效的是将模式设置为如下var pattern = "%s" + q + "%s"; 而不是使用 ilike,这会导致仅使用 like 时出现一些错误。 like: pattern
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多