Created
August 31, 2022 18:10
-
-
Save datalifenyc/37ecef4c552f906e84c523bd2cae5ce5 to your computer and use it in GitHub Desktop.
Use SvelteKit 1.0.0 to load data from The Movie Database with async - Approach 1: Using Loading data SvelteKit docs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @type {import('./$types').PageLoad} */ | |
export async function load() { | |
const api_url = 'https://api.themoviedb.org/3/movie/popular?api_key=<your_api_key>&language=en-US&page=1' | |
const response = await fetch(api_url) | |
const data = await response.json() | |
if (response.ok) { | |
return { | |
popular: data.results | |
}; | |
} else { | |
throw new Error(data); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
/** @type {import('./$types').PageData} */ | |
export let data: any; | |
console.clear() | |
// console.log(`popular_movies: ${JSON.stringify(data.popular)}`); | |
console.log(`first_title: ${data.popular[0].title}`); | |
console.log(data.popular); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions:
routes
folder.+page.js
, replace<your_api_key>
with your api key from The Movie Database (TMDB).References:
YouTube: SvelteKit For Beginners | Movie App Tutorial
Loading data • Docs • SvelteKit
TMDB: API Documentation > Movies > Get Popular
npm list: