How can I access the data I fetched with graphql?

How can I access the data I fetched with graphql?


0

So I have Nuxt 3, and Strapi with graphql. I want to get the data from Strapi via Graphql

This works for the full data:

<template>
    <div>
    <p> {{ data }}</p>
</div>
</template>

<script setup lang="ts">

    const query = gql`
    query headerPicMediaUrl {
    productsMedias{
        data{
            id
                attributes{
                    HeaderPic1{data{attributes{url}}}
                }
            }
        }
    }  
    `
    const { data } = await useAsyncQuery(query)
    const props = defineProps({
        headerurl: {
            type: String,
            required: true
        }
    })
</script>

my page then showns within the p : { "productsMedias": { "data": [ { "id": "1", "attributes": { "HeaderPic1": { "data": { "attributes": { "url": "/uploads/20230131_034_LR_2x_e21d80c0c9.webp", "__typename": "UploadFile" }, "__typename": "UploadFileEntity" }, "__typename": "UploadFileEntityResponse" }, "__typename": "ProductsMedia" }, "__typename": "ProductsMediaEntity" } ], "__typename": "ProductsMediaEntityResponseCollection" } }
but if I want more specific data and I change the template to this:

<template>
    <div>
    <p> {{ data.productsMedias }}</p>    
</div>
</template>

VS codes markes the data red and says '__VLS_ctx.data' is of type 'unknown'.
If I try to load the page now it just stays blank (so the header isnt loaded too) and the browser console throws Uncaught (in promise) TypeError: $setup.data is null

I also tried calling it like this

<template>
    <div>
    <p> {{ data['productsMedias'] }}</p>
</div>
</template>

But I get the same error/problems

What am I doing wrong here and how do I get it to work?

1 Answer
1


0

I needed to loop through the data iteration to get the data right.
So it works with

<template>
    <div v-for="x in data">
    <p> {{ x.data[0].attributes.HeaderPic1.data.attributes.url }}</p>    
</div>
</template>



Leave a Reply

Your email address will not be published. Required fields are marked *