JSON:API匹配集合及其相应的包含

时间:2019-02-07 15:16:00

标签: node.js json express json-api jsonapi-resources

将JSON:API数据集合与其各自的包含项进行匹配的最佳实践是什么?考虑下面的以下代码。...

如果我想遍历每个场所并显示每个场馆记录的所有者完整信息怎么办? JSON:API是否希望我只是在include数组中搜索匹配的所有者记录

find(included,data[$i].relationships.owner.data.id);

find()将遍历包含的数组以查找具有匹配ID的所有者作为关系对象中的收集项所有者吗?

$(data).each(function(item){
var owner = find(included,'owner', item.relationships.owner.data.id)
})

我没有找到解释此问题的资源,或者可能是我对json:api的理解不正确。如果有人可以解释这一点或指向与我的问题有关的资源。我会很感激的。

{
  "links": {
    "self": "http://127.0.0.1/api/venues?include=owner"
  },
  "data": [
    {
      "id": "5c5b49188fd33c7a989ba9b6",
      "type": "venues",
      "attributes": {
        "name": "Kreiger - Smith",
        "address": "69675 Reilly Vista",
        "location": {
          "type": "Point",
          "coordinates": [
            -112.110492,
            36.098948
          ]
        },
        "events": [
          {
            "_id": "ad52825a8f4812e92f87b8c6",
            "name": "Cool Awesome Event!",
            "user": "b3daa77b4c04a9551b8781d0",
            "id": "ad52825a8f4812e92f87b8c6"
          }
        ],
        "created_at": "2019-02-07T14:27:13.207Z",
        "updated_at": "2019-02-07T14:27:13.207Z"
      },
      "relationships": {
        "owner": {
          "data": {
            "id": "b3daa77b4c04a9551b8781d0",
            "type": "users"
          }
        }
      }
    },
    {
      "id": "5c5b49188fd33c7a989ba9b7",
      "type": "venues",
      "attributes": {
        "name": "Oberbrunner Inc",
        "address": "1132 Kenyon Stravenue",
        "location": {
          "type": "Point",
          "coordinates": [
            -112.110492,
            36.098948
          ]
        },
        "events": [
          {
            "_id": "ad52825a8f4812e92f87b8c6",
            "name": "Cool Awesome Event!",
            "user": "b3daa77b4c04a9551b8781d0",
            "id": "ad52825a8f4812e92f87b8c6"
          }
        ],
        "created_at": "2019-02-07T14:27:13.207Z",
        "updated_at": "2019-02-07T14:27:13.207Z"
      },
      "relationships": {
        "owner": {
          "data": {
            "id": "b3daa77b4c04a9551b8781d0",
            "type": "users"
          }
        }
      }
    },
    {
      "id": "5c5b49188fd33c7a989ba9b8",
      "type": "venues",
      "attributes": {
        "name": "Gibson - Muller",
        "address": "8457 Hailie Canyon",
        "location": {
          "type": "Point",
          "coordinates": [
            -112.110492,
            36.098948
          ]
        },
        "events": [
          {
            "_id": "ad52825a8f4812e92f87b8c6",
            "name": "Cool Awesome Event!",
            "user": "b3daa77b4c04a9551b8781d0",
            "id": "ad52825a8f4812e92f87b8c6"
          }
        ],
        "created_at": "2019-02-07T14:27:13.208Z",
        "updated_at": "2019-02-07T14:27:13.208Z"
      },
      "relationships": {
        "owner": {
          "data": {
            "id": "a1881c06eec96db9901c7bbf",
            "type": "users"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": "b3daa77b4c04a9551b8781d0",
      "type": "users",
      "attributes": {
        "username": "killerjohn",
        "firstname": "John",
        "lastname": "Chapman"
      }
    },
    {
      "id": "a1881c06eec96db9901c7bbf",
      "type": "users",
      "attributes": {
        "username": "numerical25",
        "firstname": "Billy",
        "lastname": "Gordon"
      }
    }
  ]
}  

这是我最好的解决方案。但是有更好的方法吗?似乎需要进行更多的编码才能找到与包含的数据相关的集合

  axios.get('http://127.0.0.1:3000/api/venues?include=owner').then(function(response) {
    var  venues = response.data.data;
    var data = response.data;
    for(x in venues) {
      var owner = data.included.find(function(element) {
        if(element.id == venues[x].relationships.owner.data.id) {
          return element;
        }
      });
    }
  });

0 个答案:

没有答案