-
-
Notifications
You must be signed in to change notification settings - Fork 220
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
TypeError: Cannot read property 'viewer' of undefined. #30
Comments
There are many possible solutions for this bug. Some of the solutions I have shared below: {viewer.name} {viewer.login} ); }} ); const Profile = () => ( {viewer.name} {viewer.login} ); }} ); |
It is really weird. Since data is set to undefined by default, We should handle that. Luckily I found your solution. Number 1 seems straightforward. Thanks |
Hi, So I was getting this same error at the same spot as mentioned above and your fixed worked. So I went on and built out the repository list and item and changed the query. I am now getting the same error:
So here is my code (I am omitting the repository.js code: Here is index.js showing the Apollo configuration `import 'dotenv/config'; // This is going to act as the 'server' and handle the const httpLink = new HttpLink({ const link = ApolloLink.from([httpLink]); const cache = new InMemoryCache(); const client = new ApolloClient({ ReactDOM.render( // If you want your app to work offline and load faster, you can change Here is my Profile.js `import React from 'react'; import Loading from '../Loading/Loading'; const GET_REPOSITORIES_OF_CURRENT_USER = gql const Profile = () => (
); export default Profile;` and here is my RepositoryList.js which is where the error happens: `import React from 'react'; import RepositoryItem from '../RepositoryItem/RepositoryItem'; import '../Repository.css' const RepositoryList = ({ repositories }) => <RepositoryItem { ...node } /> )); export default RepositoryList;` It is probably a space or a missing comma somewhere, that is usually what issues i have ends up being. I appreciate any feedback. |
So I figured it out. there was another "layer" in the data. Meaning I couldn't pass viewer.repositories i had to pass viewer.viewer.repositories |
|
What I noticed is:
|
When component did mount, there is no value in { data } returned from component. So, it generates a TypeError: Cannot read property 'viewer' of undefined.
const Profile = () => (
{({ data, loading }) => {
const { viewer } = data;
if (loading || !viewer) {
return
}
return (
{viewer.name} {viewer.login}
)
}}
);
The text was updated successfully, but these errors were encountered: