thetiti
thetiti

Reputation: 105

Can’t read siteMetadata object from gatsby-config.js multiple queries GrahQL - Gatsby ReactJS

I am trying to read the content of siteMetadata object I defined in gatsby-config.js file, from multiple queries at once using GraphQL. I have this error:

error  'siteMetadata' is not defined  no-undef

My JS file

export const pageQuery = graphql`
  query($slug: String!) {
    markdownRemark(frontmatter: { slug: { eq: $slug } }) {
      html
      frontmatter {
        date(formatString: "MMMM DD, YYYY")
        slug
        title
        logo
        about
        language
      }
    }

    site {
        siteMetadata {
          title
          description
          author
        }
      }
  } 
`

and I call

{siteMetadata.title}

Upvotes: 2

Views: 627

Answers (1)

Ferran Buireu
Ferran Buireu

Reputation: 29315

In your gatsby-config.js you should have an object called siteMetadata within the same structure than you are querying:

module.exports = {
      siteMetadata: {
        title: `Some title`,
        description: `Some description`,
        author: `Some author`,
      },
  plugins: [...]
}

Then, on your page, you have to access to data variable, which stores all the information queried.

export const YourPage= ({ data }) => {
  const yourTitle = data.site.siteMetadata.title;
  return ...
}

The problem is that you are accessing directly to siteMetadata which is not defined yet, your information is stored inside props.data.site.siteMetadata, destructuring data as a prop, it saves you one step.

Upvotes: 2

Related Questions