createResource returns undefined

Hello, goodnight everyone. Im having a problem using createResource. This function returns me undefined whenever I try to log it (console.log(data())). I tested to see that this wasn't an issue in my API by logging the response in the fetcher function and everything was as expected. The strangest thing is that I can normally render the result of this resource even though it is undefined.
const [data] = createResource(getGames);
<For each={data()}>{game => (<Game {...game}/>)}</For>
const [data] = createResource(getGames);
<For each={data()}>{game => (<Game {...game}/>)}</For>
4 Replies
joalisonx
joalisonxOP17mo ago
REEEEE
REEEEE17mo ago
The resource is undefined initially lf you are console logging in a non reactive scope, like outside the JSX or not in an effect, it will log undefined and won't react to when the resource resolves an example:
const App = () => {
const [data] = createResource(getGames);
console.log(data()) <------ will log undefined and not update

createEffect(() => {
console.log(data()) <----- will log undefined first, and then after the resource resolves/finishes fetching it will print the data
})

return(
<For each={data()}>{game => (<Game {...game}/>)}</For>
)
}
const App = () => {
const [data] = createResource(getGames);
console.log(data()) <------ will log undefined and not update

createEffect(() => {
console.log(data()) <----- will log undefined first, and then after the resource resolves/finishes fetching it will print the data
})

return(
<For each={data()}>{game => (<Game {...game}/>)}</For>
)
}
joalisonx
joalisonxOP17mo ago
Oh I didn't know that. Thank you 👏
syahzuan
syahzuan14mo ago
Just stumbled upon the same issue. Very helpful to me as well! Thanks!
Want results from more Discord servers?
Add your server