【发布时间】:2023-02-18 02:37:25
【问题描述】:
在主页(index.js 文件)上,我使用 getServerSideProps 函数
export async function getServerSideProps(context) {
axios.defaults.headers.common['Lang'] = context.locale
try {
const response = await axios.get('/index?limit=8')
return {
props: {
data: response.data
},
};
} catch (error) {
return {
props: {
error: error
},
};
}
}
以前一切正常,但现在开始出错
connect EADDRNOTAVAIL ip:443 - Local (ip:0)
尽管如果您在 useEffect () 中向同一地址发出请求 - 一切正常
尝试升级到版本 12 - 错误仍然存在
截屏
【问题讨论】:
-
我认为不同之处在于您在前端调用
useEffect,从而使用前端的主机,而在getServerSideProps函数中,您的代码在 NodeJS 服务器环境中运行,因此您需要准确指定主机而不是/index -
主机被正确替换
-
那是浏览器控制台的屏幕截图吗?
getServerSideProps在 Node.js 环境中的服务器上运行,它不会将错误记录到浏览器的控制台。该错误/失败的请求很可能来自您客户端代码中的其他地方。