react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

TypeError: Cannot read property 'id' of null

Open ilyas-shah opened this issue 4 years ago • 1 comments

Got an error when subscribing to new links.


updateQuery: (prev, { subscriptionData }) => {
        if (!subscriptionData.data) return prev;

        const newLink = subscriptionData.data.newLink;

        **const exists = prev.feed.links.find((link) => link.id === newLink.id);**

        if (exists) return prev;

        return Object.assign({}, prev, {
          feed: [newLink, ...prev.feed.links],
          count: prev.feed.links.length + 1,
          __typename: prev.feed.__typename,
        });
      },
    });

when the "linkList" page loads, the value of newLink is null. So rather than proceeding further, we return the prev/current list.


updateQuery: (prev, { subscriptionData }) => {


        if (!subscriptionData.data) return prev;

        const newLink = subscriptionData.data.newLink;

       **if (!newLink) return prev;**

        const exists = prev.feed.links.find((link) => link.id === newLink.id);

        if (exists) return prev;

        return Object.assign({}, prev, {
          feed: [newLink, ...prev.feed.links],
          count: prev.feed.links.length + 1,
          __typename: prev.feed.__typename,
        });
      },
    });

ilyas-shah avatar May 09 '20 15:05 ilyas-shah

I also found the updateQuery function is called 2 times when someone posts a link. First time with the prev data and second without prev data.

ilyas-shah avatar May 11 '20 06:05 ilyas-shah