通过 ID 和区域设置获取内容条目

Coo*_*oop 5 javascript contentful

Contentful npm 包提供对 API 的所有功能的访问。就我而言,我知道所需条目的 ID,但需要检索非默认区域设置的数据,并且我看不到任何传递区域设置选项的方法。我的查询如下所示:

const { createClient } = require('contentful');

const contentfulClient = createClient({
  accessToken: 'xxxxxxxx',
  space: 'xxxxxxx',
});

const entry = contentfulClient
  .getEntry('xxxxxx')
  .catch(console.error);
Run Code Online (Sandbox Code Playgroud)

我知道我可以执行以下操作:

const data = await contentfulClient
  .getEntries({
    content_type: 'xxxxxx'
    locale: 'cy',
    'sys.id': xxxxx,
  })
  .catch(console.error);

const [entry] = data.items;
Run Code Online (Sandbox Code Playgroud)

但这需要内容类型并返回一个条目数组,当我知道我想要的确切条目时,这似乎违反直觉。我错过了什么吗?期望它做的事情似乎是合乎逻辑的。

Gor*_*ett 2

API 文档中没有这么说,但您绝对可以locale=通过 API 使用该参数。

\n\n
\xe2\x96\xb6 curl -H "Authorization: Bearer $CONTENTFUL_ACCESS_TOKEN" https://cdn.contentful.com/spaces/$CONTENTFUL_SPACE_ID/entries/6wU8cSKG9UOE0sIy8Sk20G\n{\n  "sys": {\n    "space": {\n      "sys": {\n        "type": "Link",\n        "linkType": "Space",\n        "id": "xxxx"\n      }\n    },\n    "id": "6wU8cSKG9UOE0sIy8Sk20G",\n    "type": "Entry",\n    "createdAt": "2018-09-06T22:01:55.103Z",\n    "updatedAt": "2018-10-08T19:26:59.382Z",\n    "environment": {\n      "sys": {\n        "id": "master",\n        "type": "Link",\n        "linkType": "Environment"\n      }\n    },\n    "revision": 14,\n    "contentType": {\n      "sys": {\n        "type": "Link",\n        "linkType": "ContentType",\n        "id": "section"\n      }\n    },\n    "locale": "en-US"\n  },\n  "fields": {\n    "internalTitle": "test test test",\n    ...\n\n\xe2\x96\xb6 curl -H "Authorization: Bearer $CONTENTFUL_ACCESS_TOKEN" https://cdn.contentful.com/spaces/$CONTENTFUL_SPACE_ID/entries/6wU8cSKG9UOE0sIy8Sk20G\\?locale\\=\\*\n{\n  "sys": {\n    "space": {\n      "sys": {\n        "type": "Link",\n        "linkType": "Space",\n        "id": "xxxx"\n      }\n    },\n    "id": "6wU8cSKG9UOE0sIy8Sk20G",\n    "type": "Entry",\n    "createdAt": "2018-09-06T22:01:55.103Z",\n    "updatedAt": "2018-10-08T19:26:59.382Z",\n    "environment": {\n      "sys": {\n        "id": "master",\n        "type": "Link",\n        "linkType": "Environment"\n      }\n    },\n    "revision": 14,\n    "contentType": {\n      "sys": {\n        "type": "Link",\n        "linkType": "ContentType",\n        "id": "section"\n      }\n    }\n  },\n  "fields": {\n    "internalTitle": {\n      "en-US": "test test test"\n    },\n    ...\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

contentful JS 客户端的文档说:

\n\n
\n

参数:
\n 名称类型属性描述。\n id 字符串
\n 查询对象可选。
\n 带有搜索参数的对象。在这种方法中,它仅适用于locale.

\n
\n\n

因此,您可以将区域设置添加为第二个参数,getEntry如下所示:

\n\n
const entry = contentfulClient\n  .getEntry(\'xxxxxx\', { locale: \'en-US\' })\n
Run Code Online (Sandbox Code Playgroud)\n