通过嵌套数据环回REST API过滤器

时间:2018-11-27 08:09:23

标签: filter loopbackjs loopback

我想通过嵌套数据从REST API进行过滤。例如,该对象:

[
  {
    "name": "Handmade Soft Fish",
    "tags": "Rubber, Rubber, Salad",
    "categories": [
      {
        "name": "women",
        "id": 2,
        "parent_id": 0,
        "permalink": "/women"
      },
      {
        "name": "kids",
        "id": 3,
        "parent_id": 0,
        "permalink": "/kids"
      }
    ]
  },
  {
    "name": "Tasty Rubber Soap",
    "tags": "Granite, Granite, Chair",
    "categories": [
      {
        "name": "kids",
        "id": 3,
        "parent_id": 0,
        "permalink": "/kids"
      }
    ]
  }
]

由GET /api/products?filter[include]=categories来 而且我只想获得类别名称为“妇女”的产品。怎么办?

5 个答案:

答案 0 :(得分:2)

LoopBack不支持基于相关模型的过滤器。

不幸的是,这是我们从来没有带宽可以解决的局限性:(

有关更多详细信息,请参见此处的讨论和链接的问题:

答案 1 :(得分:1)

也许您想通过Category REST API获取此数据。例如:

GET /api/categories?filter[include]=products&filter[where][name]=woman

结果将是与所有category相关的products对象。为此,有必要在模型上声明此关系。

答案 2 :(得分:0)

请问您可以分享没有过滤器的外观吗?[include] = categorie,请吗?

[编辑] 在评论了几个问题之后,我将构建一个远程方法:在common/models/myModel.js中(在函数内部):

function getItems(filter, categorieIds = []) {
    return new Promise((resolve, reject) => {
        let newInclude;
        if (filter.hasOwnProperty(include)){
            if (Array.isArray(filter.include)) {
                newInclude = [].concat(filter.include, "categories")
            }else{
                if (filter.include.length > 0) {
                    newInclude = [].concat(filter.include, "categories");
                }else{
                    newInclude = "categories";
                }
            }
        }else{
            newInclude = "categories";
        }

        myModel.find(Object.assign({}, filter, {include: newInclude}))
        .then(data => {
            if (data.length <= 0) return resolve(data);
            if (categoriesIds.length <= 0) return resolve(data);

            // there goes your specific filter on categories
            const tmp = data.filter(
                item => item.categories.findIndex(
                    categorie => categorieIds.indexOf(categorie.id) > -1
                ) > -1
            );
            return resolve(tmp);
        })
    }
}
myModel.remoteMethod('getItems', {
    accepts: [{
        arg: "filter",
        type: "object",
        required: true
    }, {
        arg: "categorieIds",
        type: "array",
        required: true
    }],
    returns: {arg: 'getItems', type: 'array'}
});

我希望它能回答您的问题...

答案 3 :(得分:0)

Try like this.It has worked for me.

  const filter = {
          where: {
            'categories.name': {
              inq: ['women']**strong text**
            }
          }
        };

Pass this filter to request as path parameters and the request would be like bellow

GET /api/categoriesfilter=%7B%22where%22:%7B%categories.name%22:%7B%22inq%22:%5B%women%22%5D%7D%7D%7D

答案 4 :(得分:0)

您看到 ActiveMQ 管理控制台的目的是因为缺少凭据而点击松弛 URL,请跳过使用基本身份验证的凭据。我写了一个 fab 赞助人来命名休息 URL。你可以使用类似的东西。