Comments (10)
I solved the problem avoiding destructuring:
const { loading, data } = useQuery(FETCH_POSTS_QUERY);
and then:
<Grid.Row> {loading ? ( <h1>Loading Posts...</h1> ) : ( data.getPosts.map(post => ( <Grid.Column key={post.id}> <PostCard post={post} /> </Grid.Column> )) )} </Grid.Row>
if we get data from destructuring at the beginning of the component, it' s like to write something like:
const allQuery = useQuery(); const loading = allQuery.loading; const posts = allQuery.data.getPosts.posts
;
Now what do we get if 'data' in the last line is undefined? getPosts of undefined;
from classsed-graphql-mern-apollo.
Just create a variable and fetch that data in that variable and I'll solve your error.
function Home() {
let posts = "";
const { loading, data } = useQuery(FETCH_POSTS_QUERY);
if (data) {
posts = data.getPosts;
}
return (
<Grid.Row className="page__title">
Recent Posts
</Grid.Row>
<Grid.Row>
{loading ? (
Loading...
) : (
posts &&
posts.map((post) => (
<Grid.Column key={post.id} style={{ marginBottom: 20 }}>
</Grid.Column>
))
)}
</Grid.Row>
);
}
from classsed-graphql-mern-apollo.
to solve this I think you'll have to pass Home() an empty object as an argument and then assign it to the destructured data :
function Home(arg = {}) {
const {user} = useContext(AuthContext);
const {loading, data:{getPosts : posts}=arg} = useQuery(FETCH_QUERY);
from classsed-graphql-mern-apollo.
from classsed-graphql-mern-apollo.
Hi Vince I eventually figured out how to resolve my issue. I would check to make sure posts is available in the data object that you're destructuring
from classsed-graphql-mern-apollo.
I am at this juncture - TypeError: Cannot read property 'getPosts' of undefined
and really have no idea what or why this is happening since I am following a tutorial to LEARN how to do this.
Any help would be appreciated.
from classsed-graphql-mern-apollo.
Same problem - TypeError: Cannot read property 'getPosts' of undefined
from classsed-graphql-mern-apollo.
Finally, I got it.
The problem with TypeError: Cannot read property 'getPosts' of undefined
is in the CLIENT side dependencies with the new version "@apollo/react-hooks": "^4.0.0"
, somehow it returns first undefined and after an Object.
The solution is using the oldest version "@apollo/react-hooks": "0.1.0-beta.7"
, or using @apollo/client
.
from classsed-graphql-mern-apollo.
use {useEffect and useState } to solve this..
const {loading, data}= useQuery(FETCH_POSTS_QUERY);
const [Posts, setPosts] = useState([]);
useEffect(() => {
if(loading === false && data){
setPosts(data.getPosts);
}
}, [loading, data])
all the posts are now in Posts array.
from classsed-graphql-mern-apollo.
how find solution i need it !!
from classsed-graphql-mern-apollo.
Related Issues (20)
- Unhandled Rejection (Error): Cannot read property 'req' of undefined
- Uncaught (in promise) TypeError: Cannot read property 'extensions' of undefined
- Server deployment error
- TypeError: Cannot read property 'getPosts' of undefined HOT 3
- "Cannot read property 'unshift' of undefined" HOT 1
- args not defined. HOT 1
- Args not defined HOT 1
- Single Post Type Error HOT 4
- args not defined for react class 6 adding posts HOT 1
- Tab highlight issue HOT 1
- Making what you instructed in the video but still empty getPosts HOT 2
- Getting error on register form "Cannot read property 'username' of undefined. HOT 2
- deleteComment Mutation Bug
- Unable to get Single Post in graphql server
- TypeError: Cannot convert undefined or null to object HOT 3
- Error in Import semantic-ui-css
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
- useMutation response changes after reloading HOT 1
- Solving: Cast to ObjectId failed for value "" at path "_id" Error
- Invalid/Expired token
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from classsed-graphql-mern-apollo.