//routes/quotes/+page.js
export const load = async ({fetch}) => {
// First Fetch Request to Quotes
let quotes = [];
const fetchQuotes = async () => {
const resultQuotes = await fetch('https://dummyjson.com/quotes?limit=10');
const quotesData = await resultQuotes.json();
const quotes = quotesData.quotes
console.log("Q" + quotes);
return quotes;
}
// Second Fetch Request to Comments
let users = [];
const fetchUsers = async () => {
const resultUsers = await fetch('https://dummyjson.com/users?limit=10');
const userData = await resultUsers.json();
const users = userData.users;
console.log("U" + users);
return users;
}
return {
quotes: fetchQuotes(),
users: fetchUsers()
}
<!--routes/quotes/+page.svelte -->
<script>
export let data;
const { fetchQuotes } = data;
console.log(data);
</script>
<h1>Quotes</h1>
{#each fetchQuotes as item}
<h2>{item.quote}</h2>
<p>{item.author}</p>
{/each}
API calls in +page.js work ok but nothing is passed to +page.svelte for listing.
I’m new to svelte so probably a silly mistake??
Errors:
Unrecoverable HMR error in <+page>: next update will trigger a full reload
Uncaught (in promise) Error: {#each} only works with iterable values.
New contributor
alanb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.