'Sapper - inserting table data in index.svelte

I would like to put the data from the database calendar table on the home page using Sapper, but I get an error:

500 Cannot read properties of undefined (reading 'length')

In the routes/ index.svelte file I have:

<script context = "module">
     export async functions preload (page, session) {
         const result = await this.fetch ("calendar.json");
         const calendar = await result.json ();
         return {calendar};
     }
</script>

<script>
     export let calendar;
     console.log(calendar);
</script>

{# each calendar as a post}
     <ul>
         <li> {post.date} </li>
         <li> {post.hour} {post.post} </li>
     </ul>
{/each}

However, in routes / index.json.js:

import {initDB} from '@ lib / mysql';

export function get (req, res) {
   const {db} = initDB ();
   db.query (`SELECT * FROM calendar;`, (err, results, fields) => {
      if (err) throw err;
      res.writeHead (200, {
         'Content-Type': 'application / json'
      });
      res.end (JSON.stringify (results));
   });
}

Maybe I should put index.json.js somewhere else?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source