-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: undefined is not an object when calling refetch from useQuery hook #6816
Comments
Hi, recently noticed that we've the same error. Using 3.0.2 again solved our problem. @apollo/client: ^3.1.3 => ^3.0.2 |
@cc-dev-leader thanks for the tip! Will try and downgrade and see if it solves the problem. Very peculiar that it works with an older version... |
I created the sandbox, but I'm not able to reproduce the problem reliably. In my application the problem occurs after some kind of component update (cannot reproduce in sandbox). What I figured out is that it always happens after React fast-refresh (try in sandbox). https://codesandbox.io/s/apollo-client-3-nextfetchpolicy-bug-demo-ej8h5 |
@Poky85 This matches my use case too, only get it with fast refresh and not in production! |
I just ran into this upgrading from |
Just installed version 3.2.0 and still having this problem on |
it looks like its a known bug a workaround that works with Next.js: const { loading, error, data = {}, refetch: _refetch } = useQuery(MY_QUERY);
const refetch = useCallback(() => { setTimeout(() => _refetch(), 0) }, [_refetch]); |
@codelover2k Thanks. I tried your code but it does not work on |
@psamim, did you try disabling fast refresh and see if you still get the error? |
@Svarto Thanks. Yes ti solved the problem. |
this also works for me: const { loading, error, data = {}, refetch: _refetch } = useQuery(MY_QUERY);
const refetch = (args) => _refetch(args); make sure to restart your Next.js server the only problem that I have now, it that refetch fetches the correct data, but it does not re-render 😞 |
I'm using @apollo/client
|
Still running into this issue in production with: @apollo/client: 3.0.2 @codelover2k's suggestions work, but obviously isn't a great long-term solution. Any other suggestions? |
I'm seeing a similar issue when I try to call
|
@alexmngn I think in your case, the hook would be rightly unmounted. You might need to retain a reference to the function: const onSuccess = () => {
const _refetch = refetch;
setTimeout(() => {
_refetch();
}, 10000);
} Though I'm not sure this will work. Alternatively, you might try processing the callback in a redux store or similar or call the query directly on the Apollo Client. |
I am still getting this intermittently, it looks like in some cases the useQuery hook does not get called if you navigate back to the page - at that point of course these two functions are undefined. It happens even with the refetch function wrapped in a useCallback:
Would be great to get a fix as it happens in production too where a user can't refresh or fetchMore list items and everything is just stuck... |
This appears to be a continuation of the issue here: apollographql/react-apollo#3600 |
@aakagi's solution worked well for me on @apollo/client |
Any updates on this? The I have tried to use lazy queries as a workaround but it didn't work with the fetch policy set as By dealing with this error, we're being forced to use lazy queries where it isn't necessary at all |
@LauraBeatris Could you try 3.3.0-beta.16 to see if this issue goes away? |
@linmic having the same problem, but upgrading to |
I can confirm that |
@mribichich still happening to me in 3.3.0-rc.0 |
Upgrading from |
Upgrade to 3.2.7 solved the problem in my case |
This resolved the issue for me - thanks |
Upgrading to 3.2.7 did not solve this issue in my case. Whenever I save my code and it performs a fast-refresh I continue to see the same error but for fetchMore. Has anyone found a workaround? Thank you for sharing. |
3.2.9 did not solve the issue here... How is it possible to keep such a bug in the backlog ? We are talking here about a simple refetch() ... |
Upgrading to 3.3.0-rc.0 fixed this issue for me. |
I can confirm 3.3.7 fixed this. |
We are on 3.3.7 and we are seeing the same behavior when using |
Same issue as @juanstiza , on |
I did test it on a Snack though, and it works correctly. Also, to add a bit more information, I wrapped
The functionality is there, new items are fetched, and it works correctly. But the errors are still being thrown. |
Ok, small update. After creating a new Expo project without being able to reproduce the error, I can tell now that the culprit is Storybook... wonder if others have had the same issue with it. |
@juanstiza I'm seeing this issue outside of Storybook, too. And checking for |
I made a small change by safe checking fetchMore(). Work as expected and no error shown.
|
Im on |
I found a fix for my issue:
change to:
|
I had to patch |
After trying several suggestions including |
This is also happening to me when calling |
+1 |
any update? |
+1 |
The problem is still exist in 3.4.16 |
Should be fixed in 3.5! |
Intended outcome:
I have a react-native flatlist and I am dragging to refresh the current feed. I expected the original query to be refetch from the server and new data would be populated.
Same problem with fetchMore, I scroll down the flatlist and I expect query to add data to the list and continue scrolling through the additions.
Actual outcome:
I receive an error and the app crashes (both for refetch and fetchmore).
How to reproduce the issue:
Flatlist code:
typePolicy:
Query:
Versions
System:
OS: macOS 10.15.6
Binaries:
Node: 14.6.0 - /usr/local/bin/node
npm: 6.14.7 - /usr/local/bin/npm
Browsers:
Firefox: 79.0
Safari: 13.1.2
npmPackages:
@apollo/client: ^3.1.3 => 3.1.3
@apollo/link-context: ^2.0.0-beta.3 => 2.0.0-beta.3
apollo-cache-persist: ^0.1.1 => 0.1.1
The text was updated successfully, but these errors were encountered: