使用 Strapi 调用初始 Nuxt3 加载时出现 500 错误

des*_*cne 4 strapi nuxt.js vuejs3 nuxt-strapi nuxtjs3

在我的应用程序初始加载时,我在index.vue. 模板在这里:

<template>

  <div v-for="(season, index) in seasons" :key="index">
    {{ season.attributes.year }} 
  </div>
    
</template>
<script setup>
  const { find } = useStrapi()
  const {  data: seasons } = await find('seasons')
</script>
Run Code Online (Sandbox Code Playgroud)

第一次加载和刷新时的错误是:

Unhandled error during execution of setup function at
<Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > >
Run Code Online (Sandbox Code Playgroud)
GET http://localhost:3000/ 500 (Internal Server Error)
Run Code Online (Sandbox Code Playgroud)

服务器在 localhost 上响应 500。

在我消除错误并从错误状态重新路由到“/”后,我看到了数据,这是刷新和首次加载问题。

我的猜测是该useStrapi函数必须在 onMounted 或类似的东西上被触发?(但如果可能的话,我希望它由服务器端处理)

这可能也会有帮助。我的nuxt.config.ts

export default defineNuxtConfig({
    modules: [
        '@nuxtjs/strapi'
    ],
    strapi: {
        url: process.env.STRAPI_URL || 'http://localhost:1337',
        prefix: '/api',
        version: 'v4',
        cookie: {},
        cookieName: 'strapi_jwt'
    }
})
Run Code Online (Sandbox Code Playgroud)

我是组合 API 的新手,所以可能有些东西我不明白。如果我useFetch在其他 API(已部署的 API,它运行良好)上进行调用,但在 Strapi 本地主机上我总是遇到此问题。

小智 12

我有同样的问题。我通过将localhost连接字符串替换为 来修复它127.0.0.1