0

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>
7
  • Try to have a v-if="component_1.length" on your h2 element. Otherwise it will try to display data of something not fetched yet.
    – kissu
    Commented Jun 5, 2024 at 10:30
  • Thanks, I don't have the error anymore but it also doesn't display the content at all now, even after saving and refreshing the page. @kissu
    – Inès
    Commented Jun 5, 2024 at 11:27
  • Maybe you need to iterate on it with v-for since it's an array? Try component_1[0] first. Also, console.log the content of the response to see if you received everything properly.
    – kissu
    Commented Jun 5, 2024 at 11:41
  • is 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 case v-if="Object.keys(component_1).length" may be more appropriate.
    – yoduh
    Commented Jun 5, 2024 at 14:04
  • component_1 is already the first variable of the PageContent 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
    – Inès
    Commented Jun 5, 2024 at 14:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.