Gatsby 一直抱怨当我拥有 allMarkdownRemark 时无法在类型“MarkdownRemark”上查询字段“字段”

Joj*_*oji 4 javascript reactjs gatsby

我正在尝试像这个启动器一样配置我的 Gatsby 项目https://github.com/gatsbyjs/gatsby-starter-blog

在我的gatsby-node.js我有

const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)

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

  const blogPost = path.resolve(`./src/templates/blog-post.js`)
  return graphql(
    `
      {
        allMarkdownRemark(
          sort: { fields: [frontmatter___date], order: DESC }
          limit: 1000
        ) {
          edges {
            node {
              fields {
                slug
              }
              frontmatter {
                title
              }
            }
          }
        }
      }
    `
  ).then(result => {
    if (result.errors) {
      throw result.errors
    }

    // Create blog posts pages.
    const posts = result.data.allMarkdownRemark.edges

    posts.forEach((post, index) => {
      const previous = index === posts.length - 1 ? null : posts[index + 1].node
      const next = index === 0 ? null : posts[index - 1].node

      createPage({
        path: `blog${post.node.fields.slug}`,
        component: blogPost,
        context: {
          slug: post.node.fields.slug,
          previous,
          next,
        },
      })
    })

    return null
  })
}

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

  if (node.internal.type === `allMarkdownRemark`) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: `slug`,
      node,
      value,
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我尝试运行它输出此错误消息的开发服务器

 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Cannot query field "fields" on type "MarkdownRemark".

File: gatsby-node.js:8:10


 ERROR #11321  PLUGIN

"gatsby-node.js" threw an error while running the createPages lifecycle:

Cannot query field "fields" on type "MarkdownRemark".

GraphQL request:9:15
 8 |             node {
 9 |               fields {
   |               ^
10 |                 slug
Run Code Online (Sandbox Code Playgroud)

但实际上我所拥有的allMarkdownRemark不是MarkdownRemark。我完全复制了这个启动器在其gatsby-node.js文件中 所做的事情https://github.com/gatsbyjs/gatsby-starter-blog/blob/master/gatsby-node.js

不知道如何修复它

我的gatsby-config.js看起来像这样

  "gatsby-plugin-page-transitions",
    `gatsby-plugin-smoothscroll`,
    `gatsby-plugin-netlify-cms`,
    `gatsby-plugin-styled-components`,
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    `gatsby-plugin-offline`,
    `gatsby-plugin-react-helmet`,
    `gatsby-plugin-feed-mdx`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/content/blog`,
        name: `blog`,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/content/assets`,
        name: `assets`,
      },
    },
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          {
            resolve: `gatsby-remark-images`,
            options: {
              maxWidth: 590,
            },
          },
          {
            resolve: `gatsby-remark-responsive-iframe`,
            options: {
              wrapperStyle: `margin-bottom: 1.0725rem`,
            },
          },
          `gatsby-remark-copy-linked-files`,
          `gatsby-remark-smartypants`,
        ],
      },
    },
    {
      resolve: `gatsby-plugin-google-analytics`,
      options: {
        // edit below
        // trackingId: `ADD YOUR TRACKING ID HERE`,
      },
    }
Run Code Online (Sandbox Code Playgroud)

ksa*_*sav 6

您很可能会看到此问题,因为在gatsby-source-filesystem指向的任何路径中都找不到降价文件gatsby-config.js

根据nihgwu这个问题的评论:

MarkdownRemark节点类型只有在有markdown节点时才会创建,否则根本就没有MarkdownRemark节点类型,所以查询不到所有MarkdownRemark

要解决您的问题,请确保文件${__dirname}/content/blog夹中至少有一个 Markdown 文件。

如果你有不同的文件夹降价文件,确保添加该位置作为另一个gatsby-source-filesystem在您的项目gatsby-config.js