代码之家  ›  专栏  ›  技术社区  ›  Laurent Parmentier

环回关系从不返回值

  •  0
  • Laurent Parmentier  · 技术社区  · 8 年前

    我最近开始使用LoopBack来构建我们自己的API。它基于Mongo DB(对我来说也是很新的)。 我尝试配置多个关系(我尝试了不同的类型),但在响应中从未得到结果。 它似乎已正确配置,就好像我检查了资源管理器一样,我在示例值中看到了关系。

    例如,我有一些公司,与一个公司集团相关,如:

    - Company" Belongsto CompanyGroup
    - CompanyGroup hasMany Company
    

    这是我在模型中配置的关系

    公司内部。json

    "relations": {
        "companyGroup": {
          "type": "embedsOne",
          "model": "companyGroup",
          "foreignKey": "groupId",
          "primaryKey": "",
          "property": "group"
        }
      },
    

    在公司组中。json

    "relations": {
        "companies": {
          "type": "hasMany",
          "model": "company",
          "foreignKey": "groupId",
          "options": {
            "nestRemoting": false
          }
        }
      },
    

    如果我检查Explorer,我会看到示例值中配置的关系:

    [
      {
        "id": "string",
        "name": "string",
        "groupId": "string",
        "logo": "string",
        "isGroupManager": true,
        "createdAt": "$now",
        "updatedAt": "$now",
        "group": {
          "name": "string",
          "createdAt": "2018-03-08T14:38:51.155Z",
          "id": "string"
        }
      }
    ]
    

    但任何回应都会遗漏公司集团部分:

    {
    "id": "5a9ea3fc6d48a58bb619d180",
    "name": "Agency (BXL)",
    "groupId": "5a9ea3fc6d48a58bb619d17f",
    "createdAt": "2018-03-06T14:21:48.322Z",
    "updatedAt": "2018-03-06T14:21:48.322Z"
    

    },则,

    我配置错了什么?我应该去哪里看?我有一个最新版本的loopback 谢谢

    劳伦特

    1 回复  |  直到 8 年前
        1
  •  1
  •   Karan Raina    8 年前

    如果您在响应中获取组Id,并且希望组实例也在响应中,那么可以使用include筛选器。

    https://loopback.io/doc/en/lb3/Include-filter.html

    在您的情况下,可以使用如下所示的GET-like请求

    /api/companies/{comp_id}?filter={"include":{"group":true}}
    
    /api/companies/{comp_id}?filter={"include":["group"]}
    
    /api/companies?filter[include]=group
    
    推荐文章