Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatically create pages from data fails #63

Open
billp72 opened this issue Jan 1, 2020 · 0 comments
Open

Programmatically create pages from data fails #63

billp72 opened this issue Jan 1, 2020 · 0 comments

Comments

@billp72
Copy link

billp72 commented Jan 1, 2020

I'm trying to createpages. It works when I use gatsby develop but it fails when I use gatsby build

const Project = ({ data }) => {
  34 |   const post = data.markdownRemark;
> 35 |   const featuredImgFluid = post.frontmatter.featuredImage.childImageSharp.fluid;
     |                                                           ^
  36 |   return (
  37 |     <Layout>
  38 |       <Container>


  WebpackError: TypeError: Cannot read property 'childImageSharp' of null

  - project.js:35 Project
    src/templates/project.js:35:59

  - gatsby-ssr.js:14 Module.replaceRenderer
    gatsby-ssr.js:14:40

exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions;
  if (node.internal.type === 'MarkdownRemark') {
    const slug = createFilePath({ node, getNode, basePath: 'content' });
    createNodeField({
      node,
      name: `slug`,
      value: slug,
    });
  }
};

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions;
  const result = await graphql(`
    query {
      allMarkdownRemark {
        edges {
          node {
            fields {
              slug
            }
          }
        }
      }
    }
  `);
  result.data.allMarkdownRemark.edges.forEach(({ node }) => {
      createPage({
        path: node.fields.slug,
        component: path.resolve(`./src/templates/project.js`),
        context: {
          // Data passed to context is available
          // in page queries as GraphQL variables.
          slug: node.fields.slug,
        },
      });
  });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant