nuxt-directus: Error: Cannot read properties of undefined (reading '_data')

Version

nuxt-directus: 5.6.0 nuxt: 3.8.2

Reproduction Link

https://github.com/hi-reeve/nuxt-directus-test

Steps to reproduce

  1. use nuxt-directus with asyncData
  2. an error occured
  3. set asyncdata option to server:false
  4. data fetched

What is Expected?

  1. fetching data on serverside with no error

nuxt.config.ts

// https://nuxt.com/docs/api/configuration/nuxt-config

export default defineNuxtConfig({
    devtools: { enabled: true },
    runtimeConfig: {
        dbConnectionString: process.env.DIRECTUS_DIRECT_URL,
        directus: {
            url: process.env.DIRECTUS_APP_URL,
        },
    },
    modules: ["@nuxt/image", "@unocss/nuxt", "nuxt-directus"],
    imports: {
        dirs: ["libs/**", "composables/**"],
    },
    directus: {
        devtools: true,
        url: process.env.DIRECTUS_APP_URL,
        token: process.env.DIRECTUS_API_TOKEN,
    },
    experimental: {
        componentIslands: true,
        typedPages: true,
    },
});

the code

<script setup lang="ts">
import { type Blog } from "@/types/blog";

const { getItems } = useDirectusItems();
const {
    data: blogList,
    pending: loading,
    error,
} = await useAsyncData("bloglist", () =>
    getItems<Blog>({
        collection: "blog",
    })
);
const { data: blogListClient, pending: loadingClient } = await useAsyncData(
    "bloglist-client",
    () =>
        getItems<Blog>({
            collection: "blog",
        }),
    {
        server: false,
    }
);
</script>

<template>
    <div>
        <div>
            <p>Server Side</p>

            <div>
                {{ error }}
            </div>
            <p v-if="loading">Loading...</p>
            <pre v-else>{{ blogList }}</pre>
        </div>
        <div>
            <p>Client side</p>
            <p v-if="loadingClient">Loading...</p>
            <pre v-else>{{ blogListClient }}</pre>
        </div>
    </div>
</template>

the result image

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Comments: 23 (3 by maintainers)

Most upvoted comments

So, I can confirm that on 18.16.0 there is indeed a fetch issue, so updating it is the way to go.

image

But now that I think about it I remember reading in the nuxt 3.8.0 release a fetch rewrite using native note fetch for nitro 2.7.0, more in particular in nitro/#724.

Anyway, I would consider this issue closed since, to me, it does not seem to be directly related to nuxt-directus itself, but just a combination of node+nuxt versions

thankyou!

So, I can confirm that on 18.16.0 there is indeed a fetch issue, so updating it is the way to go. image

But now that I think about it I remember reading in the nuxt 3.8.0 release a fetch rewrite using native node fetch for nitro 2.7.0, more in particular in nitro/#724.

Anyway, I would consider this issue closed since, to me, it does not seem to be directly related to nuxt-directus itself, but just a combination of node+nuxt versions

can you help confirm this really happen when you use node 18.16.0 ?

I’ll try to test it, once I get back home

i found a solution, […] and it is works fine now.

You mean with ofetch and not only fetch native right?

Indeed I’m currently on node 20.6.1 so that might be it

yes i tried everything including axios. and the result the same. the only solution is to upgrade node version

I have a very similar problem. “createItems” throws an error “Cannot read properties of undefined (reading ‘data’) at createItems”, although creation is successful and everything works as expected.

UPD: That was my fault. After creating an item, the directus should return the ID, but it couldn’t because my access rights prohibited reading these items, only creation was allowed. And therefore, “createItems” threw an error even if successful.