如何在Contentful查询中检索链接的字段
我正在使用Contentful,并使用一系列相关字段的内容模型。 我正在使用JS api查询NodeJS中的内容。 如果我打电话来获取条目,就像这样
contentfulClient.getEntries({ content_type: 'homePage' })
它获取homePagetypes的所有内容,并包含每个相关字段的实际字段数据,如下所示
"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" } },
但是,如果我打电话给具有ID的特定条目,比如
contentfulClient.getEntry('1234567890')
然后每个链接的字段只是作为一个ID的引用返回,就像
"subField": { "sys": { "type": "Link", "linkType": "Entry", "id": "#######" } },
调用getEntry时如何获得全文,就像我使用getEntries一样 ?
不幸的是,Contentful在直接通过id获取内容项时不包括引用的内容。 解决这个问题的一个方法是改用getEntries
方法,但是用sys.id
过滤。 这样,你会得到相同的条目,虽然在一个数组中,但它也会包含引用的内容。
contentfulClient.getEntries({ content_type: 'homePage', sys.id: '1234567890' })
这也会导致一个单一的请求,而不是多个,因为当使用GetEntry
方法,然后手动parsing每个被引用的内容项时,最终会出现这个GetEntry
。