How to retrieve linked fields in Contentful query

旧城冷巷雨未停 提交于 2021-02-19 07:31:10

问题


I'm using Contentful and have a content model which uses a series of related fields. I am querying my content in NodeJS using the JS api. If I call get entries, like so

contentfulClient.getEntries({
    content_type: 'homePage'
})

it fetches all the content of type homePage, and includes the actual field data for each related field, like so

"subField": {
        "sys": {
            "space": {
                "sys": {
                    "type": "Link",
                    "linkType": "Space",
                    "id": "#######"
                }
            },
            "id": "#######",
            "type": "Entry",
            "createdAt": "2017-03-10T15:58:25.697Z",
            "updatedAt": "2017-03-10T15:58:25.697Z",
            "revision": 1,
            "contentType": {
                "sys": {
                    "type": "Link",
                    "linkType": "ContentType",
                    "id": "homeSubField"
                }
            },
            "locale": "en-GB"
        },
        "fields": {
            "category": {
                "sys": {
                    "type": "Link",
                    "linkType": "Entry",
                    "id": "#######"
                }
            },
            "subFieldContent": "Some field content"
        }
    },

However, if I call a specific entry with an ID, like

contentfulClient.getEntry('1234567890')

Then each linked field is just returned as a reference with an ID, like

"subField": {
            "sys": {
                "type": "Link",
                "linkType": "Entry",
                "id": "#######"
            }
        },

How can I get the full text when calling getEntry, like I do with getEntries?


回答1:


Unfortunately Contentful does not include referenced content when fetching a content item by id directly. One way around this is to instead use the getEntries method, but filter by sys.id. That way you'll get the same entry back, although in an array, but it will also include referenced content.

contentfulClient.getEntries({
   content_type: 'homePage',
   sys.id: '1234567890'
})

This also results in a single request instead of multiple as you would end up with when using the GetEntry method and then resolving each referenced content item manually.



来源:https://stackoverflow.com/questions/42769668/how-to-retrieve-linked-fields-in-contentful-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!