首页 文章

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

提问于
浏览
5

我正在使用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
---

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

{
  allMarkdownRemark {
    edges {
      node {
        frontmatter {
          templateKey
          nazev
          title
          cena
          price
        }
      }
    }
  }
}

我无法让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
        }
      ]
    }
  ]
}

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

1 回答

  • 10

    解决了!

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

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

相关问题