GatsbyJS以编程方式创建页面

时间:2020-06-03 17:38:09

标签: reactjs wordpress graphql gatsby wp-graphql

我正在尝试使用GatsbyJS createPages API以编程方式创建页面。我目前正在尝试使用gatsby-source-graphqlWPgraphQL从WordPress查询数据,根据GraphiQL我成功地做到了。我遵循了在GatsbyJS createPages API示例中找到的示例,但是只要我的程序进入“ .forEach”函数,我都会不断遇到此错误。

TypeError: Cannot read property 'pages' of undefined

在gatsby-node.js中找到我的CreatePage函数:

const path = require(`path`)

exports.createPages = ({ graphql, actions }) => {
    const { createPage } = actions
    const pageTemplate = path.resolve(`src/templates/page-template.js`)
    // Query for markdown nodes to use in creating pages.
    // You can query for whatever data you want to create pages for e.g.
    // products, portfolio items, landing pages, etc.
    // Variables can be added as the second function parameter
    return graphql(`
    query MyQuery {
        wpgraphql {
          pages {
            edges {
              node {
                slug
              }
            }
          }
        }
      }
  `, { limit: 1000 }).then(result => {
        if (result.errors) {
            throw result.errors
        }

        // Create pages.
        result.wpgraphql.pages.edges.forEach(edge => {
            createPage({
                // Path for this page — required
                path: `${wpgraphql.edges.node.slug}`,
                component: pageTemplate,
                context: {
                    // Add optional context data to be inserted
                    // as props into the page component..
                    //
                    // The context data can also be used as
                    // arguments to the page GraphQL query.
                    //
                    // The page "path" is always available as a GraphQL
                    // argument.
                },
            })
        })
    })
}

0 个答案:

没有答案