Search code examples
strapisveltekit

Different response on Strapi retrieving Single vs Collecton Types (in SvelteKit)


I use Strapi v4 and, when retrieving data from the frontend (SvelteKit) on http://localhost:3000/blog for example, with this code:

<script lang="ts" context="module">
    import type { Load } from '@sveltejs/kit';
    import { goto } from '$app/navigation';
    export const load: Load = async ({ fetch }) => {
        const res = await fetch(`${import.meta.env.VITE_BACKEND_URI}/api/posts?populate=*`);
        const response = await res.json();
        return { props: { posts: response.data } };
    };
</script>

<script lang="ts">
    export let posts: any;
    console.log(posts);
</script>

I get this:

console log response

With all the correct nested data.

But then, when I click on each post, for example: http://localhost:3000/blog/second-chance and I try to retrieve the data with:

<script lang="ts" context="module">
    import type { Load } from '@sveltejs/kit';
    import { goto } from '$app/navigation';
    export const load: Load = async ({ params, fetch }) => {
        const res = await fetch(
            `${import.meta.env.VITE_BACKEND_URI}/api/posts/${params.slug}?populate=*`
        );
        const response = await res.json();
        return { props: { posts: response.data } };
    };
</script>

<script lang="ts">
    export let posts: any;
    console.log('posts', posts);
</script>

I get this instead:

console log response

Where the fields author and localizations are missing (but not SEO, which is strange).

Does someone know what could be happening?


Solution

  • Found the solution, it was on the backend (Strapi) instead of the frontend (Svelte) and it had to do with the sanitizer, so removing:

    const sanitizedEntity = await this.sanitizeOutput(post);
    

    gives all the fields back.