Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用NetlifyCMS和Gatsby让slug显示在graphql中?_Gatsby_Netlify_Netlify Cms - Fatal编程技术网

如何使用NetlifyCMS和Gatsby让slug显示在graphql中?

如何使用NetlifyCMS和Gatsby让slug显示在graphql中?,gatsby,netlify,netlify-cms,Gatsby,Netlify,Netlify Cms,将netlify cms添加到站点时,如何让slug显示在graphql中? 我收集了一篇博客文章,除了slug外,所有的东西都显示出来了: backend: name: git-gateway branch: master # Branch to update (optional; defaults to master) media_folder: static/images public_folder: /images collections: - name: "blog"

将netlify cms添加到站点时,如何让slug显示在graphql中? 我收集了一篇博客文章,除了slug外,所有的东西都显示出来了:

backend:
  name: git-gateway
  branch: master # Branch to update (optional; defaults to master)

media_folder: static/images
public_folder: /images

collections:
  - name: "blog"
  label: "Blog"
  folder: "content/blogPost"
  create: true
  slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
  fields: # The fields for each document, usually in front matter
    - { label: "Layout", name: "layout", widget: "hidden", default: "blog" }
    - { label: "Title", name: "title", widget: "string" }
    - { label: "Publish Date", name: "date", widget: "datetime" }
    - { label: "Featured Image", name: "thumbnail", widget: "image" }
    - { label: "Body", name: "body", widget: "markdown" }
下面是我的graphql查询:

GraphQL查询中的
slug
不是
config.yml
中字段的前端部分。这些slug字段不相关。您在查询中引用的是Gatsby中的节点

您的
节点
中缺少上面GraphQL查询中的
字段
值。必须使用
gatsby node.js
config将其添加,如下例所示:

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

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

  if (node.internal.type === `MarkdownRemark`) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: `slug`,
      node,
      value,
    })
  }
}

这对我也有帮助。非常感谢。对于像我这样读过这个答案但不理解它的人来说,将它与来自的完整
gastby node.js
文件进行比较会有所帮助。