使用Gatsby.js中的Markdown Remark自定义frontmatter变量

Rob*_*olf 17 markdown yaml-front-matter graphql gatsby remarkjs

我正在使用Gatsbyjs和NetlifyCMS构建一个网站.我已经开始使用这个启动器https://github.com/AustinGreen/gatsby-starter-netlify-cms,我现在正在尝试自定义它.

我想在markdown文件的frontmatter中使用自定义变量,如下所示:

---
templateKey: mirror
nazev: ?ernobílá
title: Black and White
cena: '2700'
price: '108'
thumbnail: /img/img_1659.jpeg
---
Run Code Online (Sandbox Code Playgroud)

我想用GraphQL访问这些数据.我使用gatsby-source-filesystem和gatsby-transform-remark.这是我的查询:

  {
  allMarkdownRemark {
    edges {
      node {
        frontmatter {
          templateKey
          nazev
          title
          cena
          price
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我无法让GraphQL读取我自己的变量,它只识别titletemplateKey(那些已经在启动器中使用过的).我收到此错误:

{
  "errors": [
    {
      "message": "Cannot query field \"nazev\" on type \"frontmatter_2\".",
      "locations": [
        {
          "line": 7,
          "column": 11
        }
      ]
    },
    {
      "message": "Cannot query field \"cena\" on type \"frontmatter_2\".",
      "locations": [
        {
          "line": 9,
          "column": 11
        }
      ]
    },
    {
      "message": "Cannot query field \"price\" on type \"frontmatter_2\". Did you mean \"pricing\"?",
      "locations": [
        {
          "line": 10,
          "column": 11
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我搜索了几天,却一无所获.有人会帮我吗?

Rob*_*olf 35

解决了!

问题是我的markdown文件的前端中新添加的属性没有显示在我的GraphQL中.

我所要做的就是用'gatsby-develop'重启服务器.

  • 我真的很感动你几天不需要重启盖茨比.由于错误,我每5分钟重新启动一次 (4认同)
  • 只需重新启动Gatsby,让'gatsby-transformer-remark'知道你已对frontmatter做了更改:) (3认同)