#useSWR API call returning 'undefined'

26 messages · Page 1 of 1 (latest)

glass shell
#

It looks like the data is there to me? Is that data object in your browser?

hallow dock
#

I'm just showing what the data looks like that I'm trying to return

glass shell
#

Are you setting any headers in your request?

hallow dock
#

I switched the domain to a public random generator API and it works, I guess something is wrong with my API data

#

both have content-type: application/json so I'm not even sure.

glass shell
#

It may be a cors issue. Try making a request to it with Postman and see if it works

hallow dock
#

doing now, would I have to setup CORS on the api server or nextjs?

#

what's weird is that I'm using getStaticProps to pull data from the same API endpoint.

#

on another page and it works just fine

vague yoke
#

getStaticProps runs on the server.
CORS is a browser-thing.

hallow dock
#

Yeah i just realized that the same endpoint im using on getStaticProps is not working with the SWR

#

looking into cors now..thanks all

hallow dock
#

enabled CORS still getting undefined.

vague yoke
#

what's shown in your network tab?

hallow dock
#

i dont get it, nextJS is hitting the endpoint

#

console is saying "XHR finished loading: GET" though

vague yoke
#

are you using the async fetchData function? I think it probably should be async

hallow dock
#

i just added this function and it's working...wtf

#

is this hacky method?

hallow dock
#

removed the get function, adding this helped fix my issue

glass shell