React TypeError: Cannot read property ‘getDates’ of undefined Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of React TypeError: Cannot read property ‘getDates’ of undefined without wasting too much if your time.

The question is published on by Tutorial Guruji team.

today i decided to update the dependencies of my react project and my component Home didn’t work anymore, i’m actually working with a apollo client and apollo react hooks, this is mi Home component file:

function Home(props) {
    const {
        data: { getPosts: posts }
    } = useQuery(FETCH_POSTS_QUERY);

    return (
        <Grid columns={3} stackable={true} className={loading ? 'loading' : ''}>
            <Grid.Row className='page-title'>
                <h1>Recent Posts</h1>
                {user && (
                        <PostForm user={user} />
                {loading ? (
                    <Loading />
                ) : (
                    posts &&
          > (
                        <Grid.Column key={post._id} style={{ marginBottom: 20 }}>
                            <PostCard post={post} />

and i’m getting this error in the browser: “TypeError: Cannot read property ‘getPosts’ of undefined”

i’m trying to fix it with this little code variation:

function Home(props){
    let posts = '';
    const { user } = useContext(AuthContext);
    const { loading, data } = useQuery(FETCH_POSTS_QUERY);

    if (data) {
        posts = data.getPosts;

And everything works fine, but if i add a new Post updating the apollo cache, that cache update correctly with old posts and new post, but the frontend didn’t show it, only show old posts until i refresh the page manually.

Edit: This is the code from the PostForm component, i updated the Home component too adding the PostForm:

function PostForm(props) {
    const { values, handleChange, handleSubmit } = useForm(createPostCallback, {
        title: 'Example Title',
        body: ''

    const [createPost] = useMutation(CREATE_POST_MUTATION, {
        variables: values,
        update(dataProxy, result) {
            const data = dataProxy.readQuery({
                query: FETCH_POSTS_QUERY
            data.getPosts = [,];
                query: FETCH_POSTS_QUERY,
            values.body = '';

    function createPostCallback() {

Any idea how to fix the first code issue? Thanks in advance mates!


Queries for read and write cache in apollo works in a inmutable way. In order to do that, you have to use a new variable, you’re using data to write the cache. Try doing this:

  const [createPost] = useMutation(CREATE_POST_MUTATION, {
    variables: values,
    update (proxy, result) {
      const data = proxy.readQuery({
        query: FETCH_POSTS_QUERY
      const new_post = //here's the new var
        query: FETCH_POSTS_QUERY,
        data: { getPosts: [new_post,] } // here you're using that var to write the cache
      values.body = ''
We are here to answer your question about React TypeError: Cannot read property ‘getDates’ of undefined - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji