Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

SvelteKit - Nested Prop Cannot read properties of undefined (reading 'whatever')

$
0
0

Simply I don't get it. I store fetch result in writable store and retrieve those data, but I got undefined error when I want to use nested json data (school) see below. Why?

<p>item: {$data.item}</p>           <--- OK!<p>address: {$data.address}</p>     <--- OK!<p>school: {$data.city.school}</p>  <--- PROBLEM: Undefined. Why? simple nested json...Json structure:{"item": "whatever","address": "address","city": {"school": "school"}}
export default function fetchData() {    const loading = writable(false)    const error = writable(false)    const data = writable({})    let clear;    async function getData() {        loading.set(true)        error.set(false)        try {            const response = await fetch('http://endpoint')            data.set(await response.json())        } catch(e) {            error.set(e)        }        loading.set(false)    }  clearInterval(clear)  clear = setInterval(getData, 5000)    return [ data, loading, error, getData]}

In svelte file:

<script>    import snapshotStore from '../stores/dataStore';    const [data, loading, error, getData] = dataStore();</script><p>item: {$data.item}</p>           <p>address: {$data.address}</p>    <p>school: {$data.city.school}</p> <--- Problem, undefined.

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>