在 vercel 上部署后无法读取未定义的属性(读取“协议”)

问题描述 投票:0回答:2

我正在使用 axios 从 API 获取数据:

 const fetchData = async () => {
    try {
      const response = await axios.get(API_ENDPOINT)
      const res = await response.data;
      console.log(res);
      setMovie(res.results);
    } catch (error) {
      console.log(error);
    }
  }

我使用 vite 作为捆绑器,因此可以通过以下方式访问创建的环境变量:

const API_ENDPOINT = import.meta.env.VITE_URL;

它在“本地环境”中运行时获取数据。但是在 vercel 上部署之后。它没有获取数据并显示: TypeError: Cannot read properties of undefined (reading 'protocol')
我在使用 
fetch
获取 API 之前,它正在做同样的事情。

reactjs api axios vite vercel
2个回答
0
投票

尝试调试,通过编写一些测试来查看错误是否发生在

const response = await axios.get(API_ENDPOINT)

之前。

解决问题的另一个方法是建立数据库。


0
投票

“获取主项目时出错:TypeError:无法读取未定义的属性(读取‘协议’)”

提前致谢

© www.soinside.com 2019 - 2024. All rights reserved.