react-apollo
react-apollo copied to clipboard
TypeError: Cannot read property 'id' of null
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,
});
},
});
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.