Skip to content

Gatsby Pagination Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Gatsby Pagination Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

error "gatsby-node.js" returned an error
TypeError: Cannot read properties of undefined (reading 'edges')

Pagination code in gatsby-<a href="/backend/nodejs/">node.js</a> tries to access results that do not exist.

Wrong

exports.createPages = async ({ graphql, actions }) => {
  const result = await graphql(`
    query {
      allMarkdownRemark {
        edges {
          node {
            frontmatter { slug }
          }
        }
      }
    }
  `)

  const posts = result.data.allMarkdownRemark.edges
  const postsPerPage = 5
  const numPages = Math.ceil(posts.length / postsPerPage) // Errors if posts is undefined
}

Output: Cannot read properties of undefined (reading 'edges') when query fails.

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions
  const result = await graphql(`
    query {
      allMarkdownRemark {
        totalCount
        edges {
          node {
            frontmatter { slug }
          }
        }
      }
    }
  `)

  if (result.errors) throw result.errors

  const posts = result.data.allMarkdownRemark.edges
  const postsPerPage = 5
  const numPages = Math.ceil(posts.length / postsPerPage)

  Array.from({ length: numPages }).forEach((_, i) => {
    createPage({
      path: i === 0 ? '/blog' : `/blog/${i + 1}`,
      component: require.resolve('./src/templates/blog-list.js'),
      context: { limit: postsPerPage, skip: i * postsPerPage },
    })
  })
}

Output: paginated blog pages at /blog, /blog/2, /blog/3, etc.

Prevention

  • Always check result.errors before accessing result.data
  • Use totalCount in queries when available
  • Handle empty results gracefully

Common Mistakes with pagination

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world GATSBY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How do I pass pagination context to templates?

Use the context option in createPage. The context object is available as GraphQL variables in your page template, accessible via $skip and $limit.

5 to 10 posts per page balances page load time and navigation frequency. Adjust based on your content length and user preferences.

Access pageContext in your template to get skip and limit values. Calculate previousPage and nextPage paths based on the current page number.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro