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

data is undefined #8

Open
princencu94 opened this issue Apr 9, 2020 · 7 comments
Open

data is undefined #8

princencu94 opened this issue Apr 9, 2020 · 7 comments

Comments

@princencu94
Copy link

Hi Yihua mmm i ran into a problem, well i guess with the versions that i have in my package.json i keep running into the error data is undefined, even tried using Hooks but i can't seem to crack it.

@juicecultus
Copy link

Screenshot 2020-04-11 at 13 47 00

Same problem here & it's not package.json - the code is wrong as the error briefly appears on the screen of Yiihua during the course itself but he fails to address it?!?...

@juicecultus
Copy link

juicecultus commented Apr 11, 2020

@ZhangMYihua the code below works

@juicecultus
Copy link

juicecultus commented Apr 11, 2020

@princencu94 here is a working code:

...

const CollectionPageContainer = ({ match }) => (
  <Query
    query={GET_COLLECTION_BY_TITLE}
    variables={{ title: match.params.collectionId }}
  >
    {({ loading, data }) => {
      if (loading) return <Spinner />;
      const { getCollectionsByTitle } = data;
      return <CollectionPage collection={getCollectionsByTitle} />;
    }}
  </Query>
);

...

@princencu94
Copy link
Author

@juicecultus Wsup i tried that before i posted the issue and kept on getting the error so i guess it could
be that i am not loading any data from the server because i see my console is saying something about Cors Access denied

@juicecultus
Copy link

@princencu94 could be, sort CORS first then, make sure it's loaded highest up in the list of app.use()

@IlyaMrz
Copy link

IlyaMrz commented Apr 20, 2021

Yeah, looks like we should destruct collection only after we got it

const CollectionPageContainer = ({match}) => (
    <Query query={GET_COLLECTION_BY_TITLE} 
    variables={{title: match.params.collectionId}}>
        {
          ({ loading, error, data}) => {
            console.log({loading})
            console.log({data})
            console.log({error})
            if (loading) return <Spinner />
            return <CollectionPage collection={data.getCollectionsByTitle} />
           }
        }
    </Query>
)

@hazartilirot
Copy link

I've just had the same issue with it though I figured out that something with restructuring data object since the previous container worked perfectly well.

Dunno, why people don't like to use a concise expression as a ternary operator

({loading, data}) => loading 
        ? <Spinner />
        : <CollectionPage collection={data.getCollectionsByTitle} />
    }

as for Query it's possible to use a useQuery hook.

npm install @apollo/react-hooks

then import it into your file

import {useQuery} from '@apollo/react-hooks'

mind, you should do the same for Provider in index.js (just import it from "@apollo/react-hooks") that's... the rest - do as a docs says.

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

4 participants