How to make fetch request on server before sending html to client?
Hello everyone, I have a question about fetching data. I want to fetch data from an api and send html based on what I fetch.
The way I have it set up now results in the console.log(res); in the async fetchData function giving me the correct result, but the console.log(data); returns this:
Question: How do I make the sending of the html to the frontend wait until the async function has completed
What I have tried: I have already tried to make the function Home() async. This resulted in the whole component not rendering.
Help would be appreciated!
10 Replies
Thanks for the reply. I have changed my code now to this:
but instead of getting the rows from the DB, in <main>{data()}</main> I get:
"undefinedundefinedundefinedundefinedundefined"
In the api, I limited the rows to be returned to 5. So it seems it is giving me the right amount of results, but they are all undefined.
Do you know why?
you could check the actual response of the api, solid start doesn't have anything to do with this given the api response is json.
Use
If you want to prevent SolidStart from streaming the response before
getData()
has settled.It happens with all apis. Also, I have logged the response ( console.log(res);) and it is fine, but in the browser I get undefined
Because you are not using
{ deferStream: true }
the response will start streaming before the promise from the API has settled.
Once it settles Seroval will try to serialize the result to be sent to the client.
Perhaps Seroval is not able to serialize the type of data that your API is returning.
Supported TypesGitHub
seroval/docs/compatibility.md at main · lxsmnsyc/seroval
Stringify JS values. Contribute to lxsmnsyc/seroval development by creating an account on GitHub.
Thanks for the reply. I replicated my problem here: https://codesandbox.io/p/devbox/naughty-villani-mpv54m?file=%2Fsrc%2Froutes%2Findex.tsx
check the console
Ah, I see now. Thanks for the replies!