question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[^3.0.0] data is undefined while loading

See original GitHub issue

Intended outcome:

Let’s take the example from your website:

const GET_DOG_PHOTO = gql`
  query Dog($breed: String!) {
    dog(breed: $breed) {
      id
      displayImage
    }
  }
`;

function DogPhoto({ breed }) {
  const { loading, error, data } = useQuery(GET_DOG_PHOTO, {
    variables: { breed },
  });

  if (loading) return null;
  if (error) return `Error! ${error}`;

  return (
    <img src={data.dog.displayImage} style={{ height: 100, width: 100 }} />
  );
}

when the breed variable changes, it should set loading = true and return the past data until it resolves.

Actual outcome:

when the breed variable changes, it sets loading = true and returns undefined for data until it resolves.

How to reproduce the issue:

See the example above. This used to work in Apollo 2.x. I think it has to do with the cache, but there is nothing mentioned on the docs. This is indeed an app-breaking bug.

Versions

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
SachaGcommented, Apr 2, 2021

I’m a bit late to the discussion but I just wanted to mention this change broke the UI for multiple of my apps and I didn’t notice the issue for a while… If it was changed on purpose, it should certainly have been highlighted in huge red type in the Apollo Client release notes or migration guide!

7reactions
benjamncommented, Sep 18, 2020

@mrtnbroder This was an intentional change introduced in AC3 by #6566 to fix #6039, and listed as a [BREAKING] change in CHANGELOG.md:

  • [BREAKING] Results with loading: true will no longer redeliver previous data, though they may provide partial data from the cache, when available. @benjamn in #6566

It’s fair to say this hasn’t been a popular change, but the only way I can see to restore reasonable behavior (so that #6039 remains fixed) would be to provide result.previousData separately from result.data, as I proposed in https://github.com/apollographql/apollo-client/issues/6603#issuecomment-658803488.

The old behavior was ambiguous, because result.data could either be a cache result for the new variables, or a stale result for some older/different variables. If we’re going to be providing stale data when variables change, it needs to come clearly labeled (e.g. result.previousData).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property '0' of undefined - Stack Overflow
1 Answer 1 · The code as it is actually works for me, I think the problem is with jquery library not loaded...
Read more >
How To Fix Cannot read Property '0' of Undefined in JS
This is a decent solution because it will guarantee that you do not get the error at runtime which might cause problems elsewhere...
Read more >
API — Jinja Documentation (3.0.x)
Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior. Here are the possible ...
Read more >
Why does Apollo client's useQuery return undefined data?
... but data and error return undefined. I am using \@apollo/client version 3.0.0-beta.24 ... While it's loading, there's no data or error.
Read more >
How to catch a javascript error (cannot read property 0 of ...
In your case, the error you have mentioned will come if the Object is undefined, not when you try to access an non...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found