【问题标题】:Cannot query field "frontmatter" on type "MarkdownRemark" in gatsby starter blog无法在 gatsby 入门博客中查询类型“MarkdownRemark”的字段“frontmatter”
【发布时间】:2021-06-02 05:39:47
【问题描述】:

我正在设置一个gatsby-blog 并在gatsby-config.js 内部的siteMetadata 中进行了更改。

  siteMetadata: {
    title: `my blog`,
    description: `A Blog starter`,
    author: `dan`,
    authorTagline: 'Learning',
    social: {
      twitter: `asdfg`,
    },

我还删除了 content 文件夹中的所有内容,以便添加自己的博客文章。截至目前,content 是一个空文件夹。

现在,在gatsby develop,我遇到了错误。我不精通gatsby和graphql,想快点建这个博客。这是整个错误列表:

myPC@myPC:~/Downloads/gatsby-blog$ gatsby develop
success open and validate gatsby-configs - 0.046s
success load plugins - 1.021s
warn The Google Analytics plugin requires a tracking ID. Did you mean to add it?
success onPreInit - 0.026s
info One or more of your plugins have changed since the last time you ran Gatsby. As
a precaution, we're deleting your site's cache to ensure there's no stale data.
success initialize cache - 0.022s
success copy gatsby files - 0.037s
success onPreBootstrap - 0.020s
success createSchemaCustomization - 0.069s
success source and transform nodes - 0.131s
success building schema - 0.375s

 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Expected type MarkdownRemarkFieldsEnum, found frontmatter___date.

File: gatsby-node.js:7:10


 ERROR #85923  GRAPHQL

There was an error in your GraphQL query:

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

If you don't expect "fields" to exist on the type "MarkdownRemark" it is most likely a typo.
However, if you expect "fields" to exist there are a couple of solutions to common problems:

- If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server
- The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have
- You want to optionally use your field "fields" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add a least one entry with that field ("dummy content")

It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "MarkdownRemark":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions

File: gatsby-node.js:7:10


 ERROR #85923  GRAPHQL

There was an error in your GraphQL query:

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

If you don't expect "frontmatter" to exist on the type "MarkdownRemark" it is most likely a typo.
However, if you expect "frontmatter" to exist there are a couple of solutions to common problems:

- If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server
- The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have
- You want to optionally use your field "frontmatter" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add a least one entry with that field ("dummy content")

It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "MarkdownRemark":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions

File: gatsby-node.js:7:10

[
  GraphQLError: Expected type MarkdownRemarkFieldsEnum, found frontmatter___date.
      at Object.EnumValue (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/validation/rules/ValuesOfCorrectType.js:112:29)
      at Object.enter (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:324:29)
      at Object.enter (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:375:25)
      at visit (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:242:26)
      at validate (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/validation/validate.js:73:24)
      at GraphQLRunner.validate (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/query/graphql-runner.js:84:44)
      at GraphQLRunner.query (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/query/graphql-runner.js:149:25)
      at /home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/bootstrap/create-graphql-runner.js:33:37
      at /home/myPC/Downloads/gatsby-blog/gatsby-node.js:10:7
      at new Promise (<anonymous>)
      at Object.exports.createPages (/home/myPC/Downloads/gatsby-blog/gatsby-node.js:7:10)
      at runAPI (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/utils/api-runner-node.js:256:37)
      at Promise.catch.decorateEvent.pluginName (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/utils/api-runner-node.js:375:15)
      at Promise._execute (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/debuggability.js:384:9)
      at Promise._resolveFromExecutor (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/promise.js:518:18)
      at new Promise (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/promise.js:103:10) {
    locations: [ [Object] ]
  },
  GraphQLError: Cannot query field "fields" on type "MarkdownRemark".
      at Object.Field (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)
      at Object.enter (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:324:29)
      at Object.enter (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:375:25)
      at visit (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:242:26)
      at validate (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/validation/validate.js:73:24)
      at GraphQLRunner.validate (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/query/graphql-runner.js:84:44)
      at GraphQLRunner.query (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/query/graphql-runner.js:149:25)
      at /home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/bootstrap/create-graphql-runner.js:33:37
      at /home/myPC/Downloads/gatsby-blog/gatsby-node.js:10:7
      at new Promise (<anonymous>)
      at Object.exports.createPages (/home/myPC/Downloads/gatsby-blog/gatsby-node.js:7:10)
      at runAPI (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/utils/api-runner-node.js:256:37)
      at Promise.catch.decorateEvent.pluginName (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/utils/api-runner-node.js:375:15)
      at Promise._execute (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/debuggability.js:384:9)
      at Promise._resolveFromExecutor (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/promise.js:518:18)
      at new Promise (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/promise.js:103:10) {
    locations: [ [Object] ]
  },
  GraphQLError: Cannot query field "frontmatter" on type "MarkdownRemark".
      at Object.Field (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)
      at Object.enter (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:324:29)
      at Object.enter (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:375:25)
      at visit (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/language/visitor.js:242:26)
      at validate (/home/myPC/Downloads/gatsby-blog/node_modules/graphql/validation/validate.js:73:24)
      at GraphQLRunner.validate (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/query/graphql-runner.js:84:44)
      at GraphQLRunner.query (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/query/graphql-runner.js:149:25)
      at /home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/bootstrap/create-graphql-runner.js:33:37
      at /home/myPC/Downloads/gatsby-blog/gatsby-node.js:10:7
      at new Promise (<anonymous>)
      at Object.exports.createPages (/home/myPC/Downloads/gatsby-blog/gatsby-node.js:7:10)
      at runAPI (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/utils/api-runner-node.js:256:37)
      at Promise.catch.decorateEvent.pluginName (/home/myPC/Downloads/gatsby-blog/node_modules/gatsby/dist/utils/api-runner-node.js:375:15)
      at Promise._execute (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/debuggability.js:384:9)
      at Promise._resolveFromExecutor (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/promise.js:518:18)
      at new Promise (/home/myPC/Downloads/gatsby-blog/node_modules/bluebird/js/release/promise.js:103:10) {
    locations: [ [Object] ]
  }
]

 ERROR #11321  PLUGIN

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

Cannot read property 'allMarkdownRemark' of undefined

  35 | 
  36 |         // Create blog posts pages.
> 37 |         const posts = result.data.allMarkdownRemark.edges;
     |                                   ^
  38 | 
  39 |         posts.forEach((post, index) => {
  40 |           const previous =

File: gatsby-node.js:37:35



  TypeError: Cannot read property 'allMarkdownRemark' of undefined
  
  - gatsby-node.js:37 
    /home/myPC/Downloads/gatsby-blog/gatsby-node.js:37:35
  
  - From previous event:
  
  - api-runner-node.js:258 runAPI
    [gatsby-blog]/[gatsby]/dist/utils/api-runner-node.js:258:22
  
  - api-runner-node.js:375 Promise.catch.decorateEvent.pluginName
    [gatsby-blog]/[gatsby]/dist/utils/api-runner-node.js:375:15
  
  - From previous event:
  
  - api-runner-node.js:374 
    [gatsby-blog]/[gatsby]/dist/utils/api-runner-node.js:374:12
  
  - timers.js:456 processImmediate
    internal/timers.js:456:21
  
  - From previous event:
  
  - api-runner-node.js:368 
    [gatsby-blog]/[gatsby]/dist/utils/api-runner-node.js:368:11
  
  - From previous event:
  
  - api-runner-node.js:275 module.exports
    [gatsby-blog]/[gatsby]/dist/utils/api-runner-node.js:275:12
  
  - index.js:421 module.exports
    [gatsby-blog]/[gatsby]/dist/bootstrap/index.js:421:9
  
  - develop-process.js:433 async module.exports
    [gatsby-blog]/[gatsby]/dist/commands/develop-process.js:433:7
  

failed createPages - 0.145s
success createPagesStatefully - 0.094s
success onPreExtractQueries - 0.003s
success update schema - 0.045s

 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Expected type MarkdownRemarkFieldsEnum, found frontmatter___date.

File: src/pages/index.js:49:40


 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Unknown argument "fields" on field "markdownRemark" of type "Query".

GraphQL request:9:20
 8 |     }
 9 |     markdownRemark(fields: { slug: { eq: $slug } }) {
   |                    ^
10 |       id

File: src/templates/blog-post.js:77:20


 ERROR #85923  GRAPHQL

There was an error in your GraphQL query:

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

If you don't expect "fields" to exist on the type "MarkdownRemark" it is most likely a typo.
However, if you expect "fields" to exist there are a couple of solutions to common problems:

- If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server
- The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have
- You want to optionally use your field "fields" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add a least one entry with that field ("dummy content")

It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "MarkdownRemark":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions

File: src/templates/blog-post.js:81:7


 ERROR #85923  GRAPHQL

There was an error in your GraphQL query:

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

If you don't expect "frontmatter" to exist on the type "MarkdownRemark" it is most likely a typo.
However, if you expect "frontmatter" to exist there are a couple of solutions to common problems:

- If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server
- The field might be accessible in another subfield, please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have
- You want to optionally use your field "frontmatter" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add a least one entry with that field ("dummy content")

It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "MarkdownRemark":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions

File: src/templates/blog-post.js:86:7

failed extract queries from components - 0.476s
success write out requires - 0.046s
success write out redirect data - 0.013s
success Build manifest and related icons - 0.184s
success onPostBootstrap - 0.202s
⠀
info bootstrap finished - 5.922s
⠀
success run static queries - 0.058s - 3/3 51.39/s
success run page queries - 0.024s - 4/4 167.48/s
warn Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db

 ERROR #98123  WEBPACK

Generating development JavaScript bundle failed


/home/myPC/Downloads/gatsby-blog/src/pages/index.js
  49:40  error  Expected type MarkdownRemarkFieldsEnum, found frontmatter___date  graphql/template-strings

✖ 1 problem (1 error, 0 warnings)


File: src/pages/index.js

failed Building development bundle - 4.692s

帮我解决这个问题。谢谢!

【问题讨论】:

    标签: reactjs graphql gatsby


    【解决方案1】:

    您收到错误是因为您删除了 /content 文件夹,因此指向该内容的所有引用和查询现在都在破坏代码,因为无法获取您查询的内容。

    您的gatsby-node.jsblog-post.js 中有对缺失内容的引用。

    解决方案:

    • 恢复虚拟初始内容以避免代码破坏查询,并在添加新帖子后立即替换它
    • 注释所有查询和对查询对象的引用(例如在您的blog-post.js 中)。这很乏味且不推荐,因为您需要在添加新内容后立即取消注释/恢复它。

    P.S:对内容的查询引用可能类似于:

        allMarkdownRemark(
          sort: { fields: [frontmatter___date], order: DESC }
          limit: 1000
        ) {
          edges {
            node {
              fields {
                slug
              }
              frontmatter {
                title
              }
            }
          }
        }
      }
    `
    

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2020-11-27
      • 2021-11-13
      • 2020-09-24
      • 2020-10-29
      • 2021-12-09
      • 2020-07-27
      • 2019-07-05
      • 2020-07-24
      相关资源
      最近更新 更多