panr / gatsby-starter-hello-friend

Pretty basic starter for Gatsby that covers all of the essentials. All you have to do is start typing!
MIT License
165 stars 71 forks source link

"Errors: Cannot read property 'node' of undefined" When posts number is more than "post per page" #7

Closed eneim closed 5 years ago

eneim commented 5 years ago

I observe that there will be this error when in /src/posts/ folder you have more posts than config.postsPerPage value. What I have tried to reproduce this:

Run below to load your starter unmodified,

gatsby new test-hello-friend https://github.com/panr/gatsby-starter-hello-friend.git

Then copying the file /src/posts/hello.md to hello-2.md, hello-3.md etc (also change its path value to make sure each has different path), then run yarn build for each copy.

When the total number of posts in /src/posts exceeds 5 (default postPerpage), yarn build will fail with the error below:

error
The GraphQL query from /Users/<masked>/<masked>/<masked>/test-hello-friend/src/templates/tags.js failed.

Errors:
  Cannot read property 'node' of undefined

  GraphQL request (6:3)
  5: ) {
  6:   allMarkdownRemark(filter: {frontmatter: {tags: {in: [$tag]}}}, sort: {fields: [frontmatter___date], order: DESC}, limit: $limit, skip: $skip) {
       ^
  7:     edges {

URL path:
  /tag/hello-friend/2
Context:
  {
    "tag": "hello friend",
    "pageNumber": 1,
    "humanPageNumber": 2,
    "skip": 5,
    "limit": 5,
    "numberOfPages": 2,
    "previousPagePath": "/tag/hello-friend",
    "nextPagePath": ""
  }
Plugin:
  none
Query:
  query <masked>(
    $limit: Int!
    $skip: Int!
    $tag: String!
  ) {
    allMarkdownRemark(filter: {frontmatter: {tags: {in: [$tag]}}}, sort: {fields: [frontmatter___date], order: DESC}, limit: $limit, skip: $skip) {
      edges {
        node {
          id
          excerpt
          frontmatter {
            title
            date(formatString: "DD MMMM YYYY")
            path
            author
            excerpt
            tags
            coverImage {
              childImageSharp {
                fluid(maxWidth: 800) {
                  ...GatsbyImageSharpFluid
                }
              }
            }
          }
        }
      }
    }
  }

  fragment GatsbyImageSharpFluid on ImageSharpFluid {
    base64
    aspectRatio
    src
    srcSet
    sizes
  }

error Command failed with exit code 1.

If I change the postPerPage to be equal to or greater than total number of posts, it is Ok. Please take a look when you have time. Thanks.

eneim commented 5 years ago

It is interesting enough that if I change this line to be

post.frontmatter.tags.indexOf(tag) !== -1

It will work normally as expected. Have no idea ...

panr commented 5 years ago

@eneim Oh, yes... It's a bug... My fault... Of course the line should look like the one you wrote above ๐Ÿ™ˆ

Would you like to contribute? If yes, please create quick patch PR with this one-line fix ๐Ÿ˜Ž

eneim commented 5 years ago

@panr Done ๐Ÿ˜Ž