javascript - 错误:在 next.js 的生产环境中收集页面数据失败
基本上,API 在开发模式下工作正常。但当进入生产环境时,它会给我一个错误,提示数据未定义或无法收集页面数据。所以,我猜我的 API 在 getStaticPaths 和 getStaticProps 中不起作用。
正确的做法是什么?如何解决这个问题?我也尝试在 VERCEL 上操作,但它给了我相同的错误。
- info Collecting page data .AxiosError: connect ECONNREFUSED ::1:3000
at AxiosError.from (file:///E:/HUMAIL/NEXT.JS%20FOLDER/ROAD%20TO%20DEVELOPERS/road-to-developers/node_modules/axios/lib/core/AxiosError.js:89:14)
_currentUrl: 'https://localhost:3000/api/allSchedules',
[Symbol(kCapture)]: false
},
cause: Error: connect ECONNREFUSED ::1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3000
}
}
Error: Failed to collect page data for /schedules/[schedule_name]
这就是我做得如何:
这是环境文件:
MONGODB_URI=mongodb+srv://username:[email /?retryWrites=true&w=majority
NEXT_PUBLIC_API_URL_SCHEDULE=https://localhost:3000/api/allSchedules
NEXT_PUBLIC_API_URL_SKILLS=https://localhost:3000/api/getAllSkills
NEXT_PUBLIC_API_BASE_URL=https://localhost:3000
这是我的方法:
export async function getStaticPaths() {
const data = await getSchedules();
const paths = await data.map((item: any) => ({
params: {
schedule_name: item.name.toString(),
},
}));
return {
paths,
fallback: false
}
}
getSchedule 函数:
import axios from "axios";
export const getSchedules = async () => {
const api_url: string | undefined = process.env.NEXT_PUBLIC_API_URL_SCHEDULE;
const res = await axios.get(api_url as string);
const data = await res.data;
return data;
}
这些方法在开发模式下运行得非常好,但我不知道为什么在生产模式下不能运行..