I am displaying some Strapi data using Vue3, and when i save the file it displays it correctly on the page, but if i refresh it i have a TypeError error : Uncaught (in promise) TypeError: $setup.component_1.imgBg is undefined
Which seems weird to me since it could definitely reach the data since it displayed it ? And it's also displayed correctly in the console if i do a console.log with the same link, even after refreshing.
I have just started working with Vue and Strapi (and in general a beginner) so maybe I am not understanding it properly
I thought it was because it didn't populate deep enough, so i added the strapi-plugin-populate-deep Here is my script :
<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
const page = ref([]);
const component_1 = ref([]);
async function fetchPage() {
await axios.get('http://localhost:1337/api/landing-page?populate=deep').then((response) => {
console.log(response.data.data.attributes.PageContent[0].imgBg.data.attributes.url);
component_1.value = response.data.data.attributes.PageContent[0];
});
}
onMounted(async () => {
await fetchPage();
});
</script>
And here is my template :
<h2>{{ component_1.imgBg.data.attributes.url }}</h2>
v-if="component_1.length"
on yourh2
element. Otherwise it will try to display data of something not fetched yet.v-for
since it's an array? Trycomponent_1[0]
first. Also,console.log
the content of the response to see if you received everything properly.component_1
even an array after assigning the async response data? even though initialized as an array, based on the template code, it looks more like an object, in which casev-if="Object.keys(component_1).length"
may be more appropriate.component_1
is already the first variable of thePageContent
array, and I don't think my issue is where i'm fetching the data, because it finds it no problem, I just don't know why it doesn't when the page reloads. It's trying to display something it hasn't fetched like you said, but why is it ok when i save but not when i reload ? I'm new to all this it's confusing. Thanks for the help though @kissu