【问题标题】:Contentful CMS data layer does not reach my Gatsby/React blog内容丰富的 CMS 数据层无法访问我的 Gatsby/React 博客
【发布时间】:2019-01-06 21:24:28
【问题描述】:

我在 Gatsby 数据层使用本地 .md 文件,但现在我需要 Contentful CMS 集成。

const Promise = require('bluebird')
const path = require('path')

exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators

return new Promise((resolve, reject) => {
const blogPost = path.resolve('./src/templates/blog-post.js')

resolve(
  graphql(
    `
      {
        allContentfulBlog(limit: 500) {
          edges {
            node {
              id
              slug
            }
          }
        }
      }
    `
  ).then(result => {
    if (result.errors) {
      console.log(result.errors)
      reject(result.errors)
    }
    result.data.allContentfulBlog.edges.forEach(edge => {
      createPage({
        path: edge.node.slug,
        component: blogPost,
        context: {
          slug: edge.node.slug,
        },
      })
    })
    return
   })
  )
 })
}

exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
  const { createNodeField } = boundActionCreators
}

这是我的 gatsby-node.js 文件。

终端给出:

TypeError:无法读取未定义的属性“allContentfulBlog”。

在 Contentful.com 上,内容类型的名称是 Blog。

我附上了终端截图 here.

有什么提示吗?

【问题讨论】:

    标签: javascript reactjs content-management-system gatsby contentful


    【解决方案1】:

    看起来像

    TypeError:无法读取未定义的属性“allContentfulBlog”。

    的结果

    无法在“ContentfulBlog”类型上查询字段“slug”

    您能分享一下您的内容模型吗?不知道你有没有看过 gatsby-contentful-starter。它附带了一个内容模型——所以你也可以在那里看看。 :)

    【讨论】:

    • 您好,我如何在这里分享内容模型?不,我不知道 gatsby-contentful-starter 存在,谢谢,我会检查一下。
    • 对于这个问题,这里可能一些截图就足够了。但一般来说,共享内容模型content-importcontent-export 是要使用的工具。它们在 npm 上可用。 :)
    • 我最终删除了 export.onCreateNode 函数,该函数负责从我从 gatsby-starter-blog 插件获得的标题(我认为)创建 slug,一切似乎都很好。
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多