0

I have a function in a +page.js file that return { props { data } }. Data is an array. However when I try to use it in svelte with

export let data

It says that it's not an array What should I do ?

1

1 Answer 1

2

You need to ensure the array is properly imported and managed in the Svelte component. Confirm the data is indeed an array (use console.log(data)). If it expected values or undefined, double-check that:

  • The +page.js function is returning the correct data structure
  • The server or API call inside +page.js is successfully fetching the data

If data is fetched asynchronously and might not be immediately available, use the await keyword in your load function.

If everything looks good then you access the data correctly. FYR:

<!-- +page.svelte -->
<script>
  export let data;

  // Verify if data is received correctly
  console.log(data);
</script>

{#if Array.isArray(data)}
  <ul>
    {#each data as item}
      <li>{item}</li>
    {/each}
  </ul>
{:else}
  <p>Data is not an array or not loaded correctly.</p>
{/if}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.