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

Tutorial part 4 - cannot read site of undefined #2270

Closed
barnu5 opened this issue Sep 28, 2017 · 15 comments
Closed

Tutorial part 4 - cannot read site of undefined #2270

barnu5 opened this issue Sep 28, 2017 · 15 comments

Comments

@barnu5
Copy link

barnu5 commented Sep 28, 2017

After following steps from "Now let’s start querying" to "It worked!!" - it didn't work :/

https://imgur.com/a/B1Lqw

I get the fail and die error with a red bg as pasted below:

TypeError: Cannot read property 'site' of undefined
new
webpack:///src/layouts/index.js:19
http://localhost:8000/commons.js:39277:25
instantiate
webpack:////react-proxy/modules/createClassProxy.js:103
_default (http://localhost:8000/commons.js:39292:23),
http://localhost:8000/commons.js:27122:17
measureLifeCyclePerf
webpack:///
/react-dom/lib/ReactCompositeComponent.js:73
ReactCompositeComponentWrapper._constructComponentWithoutOwner
webpack:////react-dom/lib/ReactCompositeComponent.js:302
ReactCompositeComponentWrapper._constructComponent
webpack:///
/react-dom/lib/ReactCompositeComponent.js:277
ReactCompositeComponentWrapper.mountComponent
webpack:////react-dom/lib/ReactCompositeComponent.js:185
Object.mountComponent
webpack:///
/react-dom/lib/ReactReconciler.js:43

@KyleAMathews
Copy link
Contributor

Hey! Thanks for reporting this issue! Sorry you ran into trouble with the tutorial.

Could you try modifying your render function in the layout component to look like:

export default ({ children, data }) => {
  console.log(data)
  return (
  <g.Div
    margin={`0 auto`}
    maxWidth={700}
    padding={rhythm(2)}
    paddingTop={rhythm(1.5)}
  >
    <Link to={`/`}>
      <g.H3 marginBottom={rhythm(2)} display={`inline-block`}>
        {data.site.siteMetadata.title}
      </g.H3>
    </Link>
    <Link className={linkStyle} to={`/about/`}>
      About
    </Link>
    {children()}
  </g.Div>
)

@KyleAMathews
Copy link
Contributor

Then look in your console to see what's logged out.

If that doesn't work, try restarting your development server.

@KyleAMathews
Copy link
Contributor

Looks like there's a JS error now in your component.

Would you mind just starting over? It does look like something went wrong while you were copying files over. If the same error happens again, I'll check it out later to see if a bug has popped up in Gatsby recently.

@barnu5 barnu5 closed this as completed Sep 29, 2017
@oreny1
Copy link

oreny1 commented Oct 8, 2017

I have the similar problem as well.

GraphQL Error Unknown field siteMetadata on type Site

Looks like the schema is not updated (grphiql don't show the new data after restart)

Any suggestions how get around it?

Thanks

@barnu5 barnu5 reopened this Oct 16, 2017
@SilasOtoko
Copy link
Contributor

Same problem here, even after copying and pasting the code straight from the tutorial and restarting the server.

@SilasOtoko
Copy link
Contributor

I've figured it out! Well, for myself at least. The tutorials don't always specify where a gatsby-config.js should go. In tutorial 3, I had the config file in my 'src' file without issues, but after talking with KyleAMatthews I realized that it should actually be created in the root folder, not in src. After moving it to the root directory the error went away! So for Tutorial 4, gatsby-config.js must be in the root directory.

Hope this helps someone else!

@barnu5
Copy link
Author

barnu5 commented Oct 23, 2017

Confirmed resolved!

@barnu5 barnu5 closed this as completed Oct 23, 2017
@kildareflare
Copy link

kildareflare commented Oct 29, 2017

I've come across a related problem, opened new issue here: #2674

@theroncross
Copy link

Having the same issue - data is undefined (verified by logging as above).
Steps taken:

  1. copied and pasted all files for this tutorial
  2. restarted with gatsby develop with each file change looking for where the issue was
  3. object structure is fine in graphiQL ( I get the title when queried )
  4. gatsby-config.js is in root
  5. tried a different browser

@heyalbert
Copy link

heyalbert commented Jun 3, 2018

I had the same issue: TypeError: Cannot read property 'site' of undefined.
Solved it by restarting the server: gatsby develop

@althe3rd
Copy link

althe3rd commented Feb 4, 2019

Did anyone ever find a solution to this? I am getting this as well having tried everything suggested but don't see a solution suggested.

@oreny1
Copy link

oreny1 commented Feb 4, 2019

@SilasOtoko solution solved the issue for me the tl;dr is move the gatsby-config.js file to the root directory.

@AlbertWhite
Copy link

AlbertWhite commented Nov 11, 2019

For me the problem is that we should give a name to the query.

From

export const query = graphql`
  query {
    site {
      siteMetadata {
        title
      }
    }
  }
`

To

export const query = graphql`
  query SiteQuery{
    site {
      siteMetadata {
        title
      }
    }
  }
`

For me there is a problem with the source code from the tutorial.

@whiteleopard0424
Copy link

it is not working

@sizco29
Copy link

sizco29 commented Mar 14, 2020

Make sure you are under a page component because graphql works for page components and useStaticQuery works better in non-page components, that can be causing the error.

const data = useStaticQuery( graphql
query {
site {
siteMetadata {
title
}
}
}
)

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