【问题标题】:I can't connect to Contentful with Gatsby我无法通过 Gatsby 连接到 Contentful
【发布时间】:2020-08-15 01:04:45
【问题描述】:

当我尝试使用 Gatsby 连接到 Contentful 时,我收到以下错误消息:

Starting to fetch data from Contentful
info Fetching default locale

 ERROR 

Accessing your Contentful space failed.
Try setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache.

Used options:
spaceId: "*******ed"
accessToken: "*******ed"
host (default value): "cdn.contentful.com"
environment (default value): "master"
downloadLocal (default value): false
localeFilter (default value): [Function]
forceFullSync (default value): false
pageLimit (default value): 100
useNameForId (default value): true

not finished source and transform nodes - 0.320s

我在 gatsby-config.js 中的代码:

module.exports = {

  siteMetadata: {
    title: `Gatsby`,
    siteUrl: `http://localhost8000`,
    author: '****',
  },
  plugins: [
    {
      resolve: `gatsby-source-contentful`,
      options: {
        spaceId: ${`process.env.CONTENTFUL_SPACE_ID`},
        accessToken: ${`process.env.CONTENTFUL_ACCESS_TOKEN`},
        // My code only works with this syntax ${`...`}
      }
    },
  ]
}

我在这个页面上看到了Brent Arias 的回复:https://stackoverflow.com/questions/60892938/ Accessing your Contentful space failed with gatsby-source-contentful。他覆盖并创建了内容空间,并在本地重新启动了初始脚本“rg-portfolio”,该脚本本身包含内容配置。它还引用了启动器中的“contentful-data.json”文件。

我使用“hello world”启动器,所以我自己添加了 gatsby-source-contentful 插件,它没有创建“contentful-data.json”文件,我看不到任何关于这个 json 的信息plugin's documentation 上的文件。 我删除并重新创建了我的内容空间,然后我重新安装了插件,但它仍然无法正常工作......

有什么想法吗?

【问题讨论】:

  • 你能分享你的 gatsby-config.js 中的 Contentful 部分吗? :)
  • 你的gatsby-config 应该看起来像this
  • @AndyHoffman 是的,我尝试以这种方式编写代码,但我不知道为什么它不起作用。我收到一条错误消息,因为它无法将其识别为字符串。

标签: gatsby contentful


【解决方案1】:

我注意到您处理字符串文字的方式存在问题。

改变这个:

spaceId: ${`process.env.CONTENTFUL_SPACE_ID`},
accessToken: ${`process.env.CONTENTFUL_ACCESS_TOKEN`},

进入这两个(推荐):

spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,

或者这个:

spaceId: `${process.env.CONTENTFUL_SPACE_ID}`,
accessToken: `${process.env.CONTENTFUL_ACCESS_TOKEN}`,

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2023-01-08
    • 2012-11-21
    • 2020-08-08
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多