loopback.io包含多个

时间:2015-10-31 15:58:09

标签: loopbackjs strongloop

我有一个带有MongoDB数据库的loopback.io API。 我有一个名为项目的模型,其中包含2个hasMany关系

  • 项目有许多 ProjectArticle 对象
  • 项目有许多 ProjectWorkHours 对象

当我跑步时,我做了一个请求:http://0.0.0.0:3000/api/Projects?filter[include][projectArticles]

我得到以下回复,这很好

{
    "title": "Project 1",
    "description": "My project number one",
    "dateCreated": "2015-10-31T00:00:00.000Z",
    "id": "5634b3af340faf570c7e70a8",
    "projectArticles": [
      {
        "articleName": "brick",
        "quantity": 5,
        "unitPrice": 2,
        "id": "5634b9ea5ab833960c8fbf6d",
        "projectId": "5634b3af340faf570c7e70a8"
      }
    ]
  }

当我这样做时:http://0.0.0.0:3000/api/Projects?filter[include][workHours]我得到了正确答案:

{
    "title": "Project 1",
    "description": "My project number one",
    "dateCreated": "2015-10-31T00:00:00.000Z",
    "id": "5634b3af340faf570c7e70a8",
    "workHours": [
      {
        "description": "blabla",
        "startDate": "2015-10-31T00:00:00.000Z",
        "endDate": "2015-10-31T00:00:00.000Z",
        "id": "5634e11d5f6471f10d2e0dd7",
        "workHourId": "5634b3af340faf570c7e70a8"
      },
      {
        "description": "blabla 2",
        "startDate": "2015-10-31T00:00:00.000Z",
        "endDate": "2015-10-31T00:00:00.000Z",
        "id": "5634e1265f6471f10d2e0dd8",
        "workHourId": "5634b3af340faf570c7e70a8"
      }
    ]
  }

但我如何在一个请求中合并这两个包含?所以我得到以下结果:

{
    "title": "Project 1",
    "description": "My project number one",
    "dateCreated": "2015-10-31T00:00:00.000Z",
    "id": "5634b3af340faf570c7e70a8",
    "workHours": [
      {
        "description": "blabla",
        "startDate": "2015-10-31T00:00:00.000Z",
        "endDate": "2015-10-31T00:00:00.000Z",
        "id": "5634e11d5f6471f10d2e0dd7",
        "workHourId": "5634b3af340faf570c7e70a8"
      },
      {
        "description": "blabla 2",
        "startDate": "2015-10-31T00:00:00.000Z",
        "endDate": "2015-10-31T00:00:00.000Z",
        "id": "5634e1265f6471f10d2e0dd8",
        "workHourId": "5634b3af340faf570c7e70a8"
      }
    ],
    "projectArticles": [
      {
        "articleName": "brick",
        "quantity": 5,
        "unitPrice": 2,
        "id": "5634b9ea5ab833960c8fbf6d",
        "projectId": "5634b3af340faf570c7e70a8"
      }
    ]
  }

我试过这个:http://0.0.0.0:3000/api/Projects?filter[include][workHours]&[include][projectArticles]但它只适用第一个,第二个过滤器被忽略了

任何想法?

1 个答案:

答案 0 :(得分:4)

我找到了这样做的语法

 http://0.0.0.0:3000/api/Projects?filter={"include":["workHours", "projectArticles"]}