Skip to content

Unable to access children of response data when using Sapper #9

@saturnonearth

Description

@saturnonearth
<script context="module">
  export async function preload({ params }) {

    const res = await this.fetch('example.com/id1234.json');
    const resJSON = await res.json();

    return { resJSON };
  }
</script>

Then later in the code...

const queryResult = useQuery('characterData', async () => {
  const data = await axios.get('example.com/id1234.json')
  return data
},{refetchInterval: 30000, initialData: menuData, refetchIntervalInBackground: false, notifyOnChangeProps: ['data', 'error']})

Then...

{#if $queryResult}
  {JSON.stringify($queryResult.data.characters)}
{/if}

will result in a the initial data showing for a split second and then it showing 'undefined'. What's weird is that if I just output the entire $queryResult.data it will not show undefined, but the entire object. I hope this makes sense.

EDIT: I found the issue! I needed to return { data }; instead of return data from axios, 🤦‍♂️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions