【问题标题】:Error with multiple queries for createPages in gatsby-node.js在 gatsby-node.js 中多次查询 createPages 时出错
【发布时间】:2021-07-25 13:57:15
【问题描述】:

我尝试了各种我能想到的格式:

const { projects, blogs }, const { data } 然后通过 data.projects 调用它...

有人能指出我的代码中的错误吗?

错误为 Syntax Error: Expected Name, found ":"。

{
      projects: allMarkdownRemark(
        filter: { frontmatter: { type: { eq: "project" } } }

谢谢

完整代码:

const path = require("path")

exports.createPages = async ({ graphql, actions }) => {
  const result = await graphql(`
    {
      projects: allMarkdownRemark(
        filter: { frontmatter: { type: { eq: "project" } } }
      ) {
        nodes {
          frontmatter {
            slug
          }
        }
      }
      blogs: projects: allMarkdownRemark(
        filter: { frontmatter: { type: { eq: "blog" } } }
      ) {
        nodes {
          frontmatter {
            slug
          }
        }
      }
    }
  `)

  result.data.projects.allMarkdownRemark.nodes.forEach(node => {
    actions.createPage({
      path: "/projects/" + node.frontmatter.slug,
      component: path.resolve("./src/templates/project-details.js"),
      context: { slug: node.frontmatter.slug },
    })
  })

  result.data.blogs.allMarkdownRemark.nodes.forEach(node => {
    actions.createPage({
      path: "/blogs/" + node.frontmatter.slug,
      component: path.resolve("./src/templates/blog-details.js"),
      context: { slug: node.frontmatter.slug },
    })
  })
}

【问题讨论】:

    标签: node.js reactjs graphql gatsby


    【解决方案1】:

    您将allMarkdownRemark 别名为projectsblogs,因此您的嵌套结构应该是:

    const path = require("path")
    
    exports.createPages = async ({ graphql, actions }) => {
      const result = await graphql(`
        {
          projects: allMarkdownRemark(
            filter: { frontmatter: { type: { eq: "project" } } }
          ) {
            nodes {
              frontmatter {
                slug
              }
            }
          }
          blogs: allMarkdownRemark(
            filter: { frontmatter: { type: { eq: "blog" } } }
          ) {
            nodes {
              frontmatter {
                slug
              }
            }
          }
        }
      `)
    
      result.data.projects.nodes.forEach(node => {
        actions.createPage({
          path: "/projects/" + node.frontmatter.slug,
          component: path.resolve("./src/templates/project-details.js"),
          context: { slug: node.frontmatter.slug },
        })
      })
    
      result.data.blogs.nodes.forEach(node => {
        actions.createPage({
          path: "/blogs/" + node.frontmatter.slug,
          component: path.resolve("./src/templates/blog-details.js"),
          context: { slug: node.frontmatter.slug },
        })
      })
    }
    

    您的新嵌套结构省略了allMarkdownRemark 节点。此外,您在第二个别名 (blogs) 中有错字。

    【讨论】:

    • 我爱你,非常非常!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2021-07-20
    • 2021-02-05
    • 2021-07-25
    相关资源
    最近更新 更多