I'm pretty new to Svelte and I'm trying to render my data object in the browser. I'm not sure what's going on as the backend seems to be okay and I'm able to print the data object in the console.
I have a backend flask api that reads the following url
https://api.le-systeme-solaire.net/rest/bodies/
That's up and working fine. If you look it up you can see what the json looks like
This is the top half of my index.svelte file:
<script>
let data = []
async function initFetchSolarSystemData() {
let response = await fetch("http://127.0.0.1:5000/solar-system/gravity");
data = await response.json();
console.log("data", data)
console.log(["id", data.bodies[0].id])
console.log(["gravity", data.bodies[0].gravity])
console.log(["bodies", data.bodies.length])
}
initFetchSolarSystemData()
</script>
So when it runs, I can see values in the console like:
['id', 'lune']
['gravity', 1.62]
'bodies', 287]
So that seems to be working fine as well. Here is the bottom portion of the file where I believe the error is stemming from:
<section>
{#if data.length}
{#each data.bodies as item}
<div class="gravity">
<h1 id="item">
{item.gravity}
</h1>
</div>
{/each}
{/if}
</section>
When the I run the script, I get no errors in the console. Nothing renders. If I replace this line:
{#if data.length}
with
{#if data.bodies.length}
I get:
Cannot read properties of undefined (reading 'length')
Which I'm confused about because these values are being read earlier above in the file.
I think I'm missing something here, and any help would be immensely appreciated.