[英]Nuxt3 ECONNREFUSED on refresh
I'm upgrading a pubblic GitHub repo (I use that base on a private one, I'm using this one to help me with the project migration) with Nuxt 3 but I'm having some issues fetching data on reload.我正在使用Nuxt 3 升级一个公共 GitHub 存储库(我在私有存储库上使用它,我正在使用这个来帮助我进行项目迁移),但我在重新加载时遇到了一些获取数据的问题。
I have a reverse proxy to redirect to the correct docker image.我有一个反向代理来重定向到正确的 docker 图像。
I'm using this code on the page:我在页面上使用此代码:
<script setup>
const runtimeConfig = useRuntimeConfig()
const { data: app, pending, error, refresh } = await useAsyncData(() => $fetch(`${runtimeConfig.public.BASE_API_BROWSER_URL}/message`))
the same goes if I use ...await useFetch...
instead of the useAsyncData
如果我使用
...await useFetch...
而不是useAsyncData
,情况也是如此
Very simple but it seems to work only when I made some changes in the code and then see the page loaded with HMR, if I refresh the page I get the following error.非常简单,但它似乎只有在我对代码进行一些更改然后看到加载了 HMR 的页面时才起作用,如果我刷新页面,我会收到以下错误。
Error: request to http://localhost/api/message failed, reason: connect ECONNREFUSED 127.0.0.1:80 ()
错误:请求 http://localhost/api/message 失败,原因:connect ECONNREFUSED 127.0.0.1:80 ()
that URL is perfectly working, if I copy and paste it I get the correct response, also as I said above, it does work if I made a change a change in the code and save the file, with HMR is working fine, it just doesn't work when I refresh the page.该 URL 工作正常,如果我复制并粘贴它,我会得到正确的响应,也正如我上面所说,如果我更改代码并保存文件,它确实有效,HMR 工作正常,它只是当我刷新页面时不起作用。
I found out that I could do this我发现我可以做到这一点
if (process.client && error.value) {
await refresh()
}
but that still doesn't answer why on refresh that doesn't work but with HMR yes.但这仍然不能回答为什么在刷新时不起作用但使用 HMR 是。
Apparently Nuxt3 and $fetch
work differently, while in Nuxt2 a code like this work:显然 Nuxt3 和
$fetch
的工作方式不同,而在 Nuxt2 中,这样的代码工作:
async asyncData ($axios, store, req }) {
const userEmail = //get email from cookie
await $axios
.post(`${BASE_API_URL}/pages/settings`, {
email: userEmail
})
more code...
In Nuxt3 with $fetch
you need to use the container name在带有
$fetch
的 Nuxt3 中,您需要使用容器名称
<script setup>
const runtimeConfig = useRuntimeConfig()
const { data: app, pending, error, refresh } = await useAsyncData(() => $fetch(`${runtimeConfig.public.BASE_API_URL}/message`))
These are the ENV这些是环境
NUXT_PUBLIC_BASE_API_URL=http://webserver/api
NUXT_PUBLIC_BASE_API_BROWSER_URL=http://localhost/api
I don't know the technical answer but I make it work.我不知道技术答案,但我让它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.