javascript - 当我有 allMarkdownRemark 时, Gatsby 一直提示无法在类型 "fields"上查询字段 "MarkdownRemark"

标签 javascript reactjs gatsby

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

在我的 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,
    })
  }
}

但是我尝试运行它输出此错误消息的开发服务器
 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

但实际上我拥有的是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`,
      },
    }

最佳答案

您很可能会看到此问题,因为在 gatsby-source-filesystem 的任何路径中都找不到 Markdown 文件。指向 gatsby-config.js
根据nihgwuthis issue 的评论:

the MarkdownRemark node type will only be created when there is a markdown node, or there will be no MarkdownRemark node type at all, so you can't query allMarkdownRemark



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

如果您在其他文件夹中确实有 Markdown 文件,请确保将该位置添加为另一个 gatsby-source-filesystem输入您的 gatsby-config.js .

关于javascript - 当我有 allMarkdownRemark 时, Gatsby 一直提示无法在类型 "fields"上查询字段 "MarkdownRemark",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62209671/

相关文章:

javascript - 为什么我不能使用 CSS 在这个特定的 Input 元素上使用固定定位?

javascript - 访问 react 中动态创建的元素集

Gatsby项目给出了奇怪的错误,无法构建

javascript - 将事件应用于导出按钮时 HighCharts 中缺少点

javascript - 像滚动行为一样聊天(ReactJs)

javascript - 如何在网站上放置透明 Flash 并保持底层网站可用(焦点、点击、表单提交等)

javascript - 收到非 bool 属性 Reactjs 的 `true`

javascript - 如何使用自定义单元格组件在 React Native 中实现 ListView?

javascript - React 条件渲染在一个地方工作,但在同一组件的另一个地方不起作用

javascript - Velocity.js:如何停止队列但使元素进入其最终状态?