TransWikia.com

NuxtJs Axios rendering issue undifined

Stack Overflow Asked on January 5, 2022

When I try to access certain objects/property loaded from an axios call inside the document,

I get undefined property, even though that the master object is being rendered just fine.

Here is what I have tried:
index.vue:

<template>
    <div>
        {{ pageContent.Cover.formats.large.url}}
    </div>
</template>
<script>
    export default {
        data() {
            return {
                baseUrl: process.env.API_URL,
                pageContent: '',
            }
        },
        created() {
            this.$axios.$get(process.env.API_URL + '/about')
                .then(response => (this.pageContent = response))
        }
    }
</script>

This gives me following error:

TypeError
Cannot read property ‘formats’ of undefined

This is the object:

enter image description here

One Answer

The callback response parameter often comes with multiple fields like data so you should do :

.then(response => (this.pageContent = response.data))

then in the template add a conditional rendering :

<div v-if="pageContent">
        {{ pageContent.Cover.formats.large.url}}
    </div>

Answered by Boussadjra Brahim on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP