Fetcher obtains data, but data and error are undefined
See original GitHub issueLove the simplicity of this library. I’m having some trouble in the handoff of the data from the fetcher to useSWR.
My setup
- Typescript
- NextJS
- Fetcher - GraphQL Client (fetch.ts)
require("fetch-cookie/node-fetch")(require("node-fetch"))
export default async function(query, variables, token) {
const data = await graphql.request(query, variables)
console.log("fetch.ts ", data)
return data
}
- SWRConfig
<SWRConfig value={{ fetcher: (query, variables, token) => fetch(query, variables, token) }}>
<SWRConfig value={{ fetcher: (query, variables, token) => fetch(query, variables, token).then(res => console.log(res)) }}>
Both setups return valid data.
- useSWR
const { data, error } = useSWR<DataQueryType>([GraphQLQuery, props, token])
Data and error remains undefined despite the fetch response being successful on the server and fetch client.
Refreshing the page doesn’t appear to help either. It seems like the useSWR isn’t passing the result to the data object.
Happy to gather additional logs/details.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Data fetched from Firebase using useSWR returns undefined
the useSWR hook accepts a key string and a fetcher function. key is a unique identifier of the data (normally the API URL)...
Read more >Error handling - Apollo GraphQL Docs
A client sent the hash of a query string to execute via automatic persisted queries, but the query was not in the APQ...
Read more >useswr cannot read properties of undefined - You.com
I'm trying to fetch data via UseSWR, which works fine. But when i call it again on click in my app, this error...
Read more >Cannot read properties of undefined (reading 'id') - TrackJS
id is commonly used on HTMLElement , but it could be a custom data object from a remote API as well. This is...
Read more >How to Prevent the Error: Cannot Read Property '0' of Undefined
Changing API Responses · The API can return undefined if no data is available. · A poorly designed API can return undefined because...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @jasonlor! I forked your sandbox and fixed the issue: https://codesandbox.io/s/beautiful-thunder-7lylp
There’re 2 problems, one is that you shouldn’t pass
{}
to useSWR’s args:Because SWR shallowly compares those args, just like the deps array in
useEffect
(explained here: https://github.com/zeit/swr#multiple-arguments).And the other one is the extra
.data
here:Hope that helps!
Let me know if you have other questions 🙏