【问题标题】:Gatsby custom Remark frontmatter variables not being passed into index.jsGatsby 自定义备注 frontmatter 变量未传递到 index.js
【发布时间】:2022-01-23 14:59:18
【问题描述】:

关于 StackOverflow 的第一个问题!

使用 Gatsby blog template,我修改了 graphql 查询并验证它在 GraphiQL 中返回了正确的数据,该数据是从博文 frontmatter 中的“redirect:”属性中提取的。

不幸的是,在运行 index.js 文件时它没有被传入数据。

gatsby-config.js

feeds: [
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => {
              return allMarkdownRemark.nodes.map(node => {
                return Object.assign({}, node.frontmatter, {
                  description: node.excerpt,
                  redirect: node.frontmatter.redirect,
                  date: node.frontmatter.date,
                  url: site.siteMetadata.siteUrl + node.fields.slug,
                  guid: site.siteMetadata.siteUrl + node.fields.slug,
                  custom_elements: [{ "content:encoded": node.html }],
                })
              })
            },
            query: `
              {
                allMarkdownRemark(
                  sort: { order: DESC, fields: [frontmatter___date] },
                ) {
                  nodes {
                    excerpt
                    html
                    fields {
                      slug
                    }
                    frontmatter {
                      redirect
                      title
                      date
                    }
                  }
                }
              }
            `,
            output: "/rss.xml",
          },
        ],

gatsby-node

type Frontmatter {
      redirect: String
      title: String
      description: String
      date: Date @dateformat
    }

我的代码库,https://github.com/tomvaillant/my_blog

感谢大家的支持!

【问题讨论】:

    标签: graphql markdown gatsby


    【解决方案1】:

    您需要像在gatsby-config 中一样查询redirect 字段。你的query 应该是这样的:

    export const pageQuery = graphql`
      query {
        site {
          siteMetadata {
            title
          }
        }
        allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
          nodes {
            excerpt
            fields {
              slug
            }
            frontmatter {
              date(formatString: "MMMM DD, YYYY")
              title
              description
              redirect # <-- here
            }
          }
        }
      }
    `
    

    【讨论】:

    • 非常感谢!我试图找到从哪里传递数据参数,但没有注意到该查询......非常感谢。
    • 我很乐意提供帮助。如果问题已得到解决,请考虑接受/投票以关闭主题
    • 嘿 Ferran,StackOverflow 流程的新手。你的回答被采纳了吗?我投了赞成票,希望一切顺利?
    • 不是的,你点赞的地方有一张支票,你需要点一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2019-05-27
    • 2022-11-07
    • 2019-11-07
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多