hidjou / classsed-graphql-mern-apollo

408 stars 255 forks source link

Single Post Error #10

Open likwid23 opened 4 years ago

likwid23 commented 4 years ago

× TypeError: Cannot read property 'getPost' of undefined

const { data: { getPost } } = useQuery(FETCH_POST_QUERY, { variables: { postId

aindriu80 commented 4 years ago

As answered in the other question:

const { data = {} } = useQuery(FETCH_POST_QUERY, { variables: { postId, }, }); const thisPost = data.getPost;

....

let postMarkup; if (!thisPost) { postMarkup =

Loading Post...

; } else { const { id, body, createdAt, username, // comments, likes, likeCount, commentCount, } = thisPost;

Works but gets stuck on Loading...

dramanesd commented 4 years ago

@aindriu80 you have to return only datalike this: const { data } = useQuery(FETCH_POST_QUERY, { variables: { postId, }, });

jhaji2911 commented 3 years ago

@likwid23 @aindriu80 the best fix I figured out is just pass an empty object to the singlePost() function then assign the destructed data to it:

function SinglePost(props,args={}) { const postId = props.match.params.postId; const { user } = useContext(AuthContext); const commentInputRef = useRef(null); const [comment, setComment] = useState(''); const{ data:{getPost} = args } = useQuery(FETCH_POST, { variables: { postId } });

Ozkilic commented 3 years ago

... const { loading, data } = useQuery(FETCH_POST, { variables: { postId, }, })

let getPost if (!loading) { getPost = data.getPost }

...

nikchhetri commented 3 years ago

I did this and it worked without any error

const postId = props.match.params.postId const { user } = useContext(AuthContext) const { loading, data} = useQuery(FETCH_POST_QUERY, { variables: {postId} } ) if (loading){return (

Loading

)}; //this is where i added a line
const {id,
body, createdAt, username, comments, likes, likeCount, commentCount } = data.getPost let postMarkup

and then just define postMarkup and continue like he does.

reddymahendra52 commented 3 years ago

@likwid23 @aindriu80 the best fix I figured out is just pass an empty object to the singlePost() function then assign the destructed data to it:

function SinglePost(props,args={}) { const postId = props.match.params.postId; const { user } = useContext(AuthContext); const commentInputRef = useRef(null); const [comment, setComment] = useState(''); const{ data:{getPost} = args } = useQuery(FETCH_POST, { variables: { postId } });

Thank you so much for this solution!! working fine for me.