javascript - 按 ID 和语言环境获取内容条目

标签 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);

我知道我可以做到以下几点:

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

const [entry] = data.items;

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

最佳答案

API documentation 上没有这样说但您完全可以通过 API 使用 locale= 参数。

▶ curl -H "Authorization: Bearer $CONTENTFUL_ACCESS_TOKEN" https://cdn.contentful.com/spaces/$CONTENTFUL_SPACE_ID/entries/6wU8cSKG9UOE0sIy8Sk20G
{
  "sys": {
    "space": {
      "sys": {
        "type": "Link",
        "linkType": "Space",
        "id": "xxxx"
      }
    },
    "id": "6wU8cSKG9UOE0sIy8Sk20G",
    "type": "Entry",
    "createdAt": "2018-09-06T22:01:55.103Z",
    "updatedAt": "2018-10-08T19:26:59.382Z",
    "environment": {
      "sys": {
        "id": "master",
        "type": "Link",
        "linkType": "Environment"
      }
    },
    "revision": 14,
    "contentType": {
      "sys": {
        "type": "Link",
        "linkType": "ContentType",
        "id": "section"
      }
    },
    "locale": "en-US"
  },
  "fields": {
    "internalTitle": "test test test",
    ...

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

documentation for the contentful JS client说:

Parameters:
Name Type Attributes Description. id string
query Object optional.
Object with search parameters. In this method it's only useful for locale.

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

const entry = contentfulClient
  .getEntry('xxxxxx', { locale: 'en-US' })

关于javascript - 按 ID 和语言环境获取内容条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54864325/

相关文章:

javascript - 如何将数组中的值映射到对象数组?

javascript - 何时以及为何使用 &?、=?、@?在 AngularJS 中?

javascript - 如何使用 nextjs 和 contentful 按年/月获取帖子?

javascript - 循环内的异步调用

javascript - 清除数据表后显示加载 gif

javascript - 将饼图名称显示为标题

javascript - 计算两个输入类型之间的天数 = "date"in html

javascript - Contentful:使用 Content Management API 时防止触发 Webhook

graphql - 如何从2个API查询数据

javascript - Gatsby.js 的 GraphQL 查询回调