在 gatsby-node.js 中多次查询 createPages 时出错

Error with multiple queries for createPages in gatsby-node.js

我已经尝试用我能想到的各种方式来格式化它:

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

调用它

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

错误显示语法错误:预期名称,找到“:”。

{
      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 },
    })
  })
}

您将 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)。