Next.js Discord

Discord Forum

How to delay response output on useSWR?

Unanswered
English Lop posted this in #help-forum
Open in Discord
English LopOP
Hi! I have a file named useOpenLibrary.js file that I use to fetch all my OpenLibrary API requests.

First issue is that when I call a function from said file, e.g. OLGetAuthor( id ), I receive the data after receiving a warning that the data is first undefined? It seems as though the data is being sent back undefined before it is even fetched? (It DOES eventually come through, but only AFTER the error.) The error is GET https://openlibrary.org/authors/undefined.json 404 (Not Found).

Second issue is that I have a .map for links received in the JSON file. As the response is first undefined, the function comes back as an error. I know this also relates to the first issue, but just another little problem I'm having.

Third is that I am unable to get the isLoading state to return a string while awaiting data. Is this due to it being from a separate file or am I not looking at this right?

If I run the function on the same page.tsx, the second issue doesn't seem to happen.

I am still fairly new to all this NextJS stuff, so trying to wrap my head around it all 😅

Any help is much appreciated!

0 Replies