Expose Post Tag Data for a Gatsby Blog

Share this video with your friends

Send Tweet

In this lesson we will prepare Tag browsing templates, and update gatsby-node.js to use GraphQL to query for tags in our Markdown posts.

Josh Schoen
Josh Schoen
~ 5 years ago

The posts forEach snippet within the transcript is not defined properly. It is correct in the video however.

shows:

posts.forEach(({node})) => {
  if (node.frontmatter.tags.forEach(tag => {
    if(!postByTag[tag]) {
    postByTag[tag] = []
  }

  postsByTag[tag].push(node)
  })
}

Should be:

  posts.forEach(({node}) => {
    if (node.frontmatter.tags) {
      node.frontmatter.tags.forEach(tag => {
        if(!postsByTag[tag]) {
          postsByTag[tag] = []
        }
        postsByTag[tag].push(node)
      })
    }
  })