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.

Fetcher obtains data, but data and error are undefined

See original GitHub issue

Love 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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
shudingcommented, Nov 17, 2019

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:

useSWR([query, {}, "bleh"])

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:

{data.data.country.name}

Hope that helps!

0reactions
shudingcommented, Nov 22, 2019

Let me know if you have other questions 🙏

Read more comments on GitHub >

github_iconTop 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 >

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