Open lacykerel opened 4 years ago
Please help!
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
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.
Same problem - TypeError: Cannot read property 'getPosts' of undefined
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
.
I solved the problem avoiding destructuring:
const { loading, data } = useQuery(FETCH_POSTS_QUERY);
and then:
`
) : (
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;
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 (
); }
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.
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);
how find solution i need it !!
I can't seem to find out where to define this or pass into my login file and it keeps returning undefined