0
votes

I'm trying to get the name of a game from the page parameters. I was expecting that the load function would get the location of the game and return it to the page but instead when I ran the code below I got this error:

Cannot read property 'slug' of undefined

This is the code that I am trying to run:

<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load({ params, fetch, session, stuff }) {
    let request = await fetch("/json/games.json");
    let games = await request.json();
    let game = games[params.slug];
    if (game && game.location) {
      return {
        props: {
          game: game.location
        }
      }
    } else {
      return {
        status: 404,
        error: "The game could not be found"
      }
    }
}
</script>

<script>
   export let game;
</script>

<svelte:head>
   <title>Game</title>
</svelte:head>

<p>Location of Game:</p>
<p>{game}</p>
1
This should be working if the name and location of your page is, say, /src/routes/games/[slug].svelte. Is that the case? - Thomas Hennes
@ThomasHennes Yes, but it is not working. - DinoNuggieLover
Are you using a recent svelte-kit version? Your code snippet should work in 1.0.0-next.294 (assuming we are looking at code from route file like src/routes/game/[slug].svelte) - Bob Fanger
As the others say: this should work. Can you also provide the url in the browser and the full path of the route itself ? - Stephane Vanraes
@BobFanger Thanks! I upgraded SvelteKit and it works - DinoNuggieLover

1 Answers

0
votes

The issue was resolved by upgrading the @sveltejs/kit package.