如何求解Vercel 500内部服务器错误?

发布于 2025-01-26 11:38:35 字数 2080 浏览 2 评论 0原文

我创建了一个使用MongoDB来存储用户信息和下一个实施用户来验证用户的项目。在本地主机上,这一切都是无缝工作的。以前,我的下一个实施配置有几个错误,但是现在在Vercel Live网站上工作正常。用户登录后,将其重定向到“我的项目/建议”。在此页面上,我使用GetServersideProps来识别是否存在有效的会话令牌。如果是这样,则将数据从本地JSON文件中获取。

在实时站点上,当用户登录时,该页面将重定向到“/建议”,但我正在收到一个500个内部服务器错误页面。在功能日志上,我收到此错误消息:

[GET] /_next/data/KpsnuV9k44lUAhQ-0rK-B/suggestions.json
10:10:57:12
2022-05-05T14:10:59.270Z    5b7a7375-045f-4518-864b-7968c3c9385f    ERROR   [Error: ENOENT: no such file or directory, open '/var/task/public/data/data.json'] {
  errno: -2,
  syscall: 'open',
  path: '/var/task/public/data/data.json',
  page: '/suggestions'
}
RequestId: 5b7a7375-045f-4518-864b-7968c3c9385f Error: Runtime exited with error: exit status 1
Runtime.ExitError

这是我使用MongoDB和Next-auth的第一个项目。在这种情况下,不确定问题是什么。在我的.env.Local文件中,我只有这两个变量:

nextauth_secret =“ munknation” nextauth_url = http:// localhost:3000

我如何在本地主机上拉数据:

export const getServerSideProps = async (context) => {
  const session = await getSession({ req: context.req });

  if (!session) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
  } else {
    let filePath = path.join(process.cwd(), "public", "data", "data.json");

    let jsonData = await fs.readFile(filePath);

    const data = JSON.parse(jsonData);

    const inProgressStatusData = data.productRequests.filter(
      (item) => item.status == "in-progress"
    );

    const liveStatusData = data.productRequests.filter(
      (item) => item.status == "live"
    );

    const plannedStatusData = data.productRequests.filter(
      (item) => item.status == "planned"
    );

    let filterData = filteredData(data, "suggestion");

    let feedbackData = {
      suggestions: filterData,
      progress: inProgressStatusData,
      planned: plannedStatusData,
      live: liveStatusData,
    };

    return {
      props: { session, feedbackData },
    };
  }
};


文件夹结构:

I have created a project that uses MongoDB to store user info and Next-Auth to authenticate users. On local host this is all working seamlessly. Previously I had a couple errors with my next-auth config, but that seems to be working fine now on Vercel live site. Once the user logs in they are redirected to "my-project/suggestions". On this page I am using getServerSideProps to identify if there is a valid session token. If so, data is pulled from a local json file.

On the live site, when the user logs in, the page is redirected to "/suggestions", yet I am receiving an 500 Internal Server Error page. On the function logs I am getting this error message:

[GET] /_next/data/KpsnuV9k44lUAhQ-0rK-B/suggestions.json
10:10:57:12
2022-05-05T14:10:59.270Z    5b7a7375-045f-4518-864b-7968c3c9385f    ERROR   [Error: ENOENT: no such file or directory, open '/var/task/public/data/data.json'] {
  errno: -2,
  syscall: 'open',
  path: '/var/task/public/data/data.json',
  page: '/suggestions'
}
RequestId: 5b7a7375-045f-4518-864b-7968c3c9385f Error: Runtime exited with error: exit status 1
Runtime.ExitError

This is my first project using MongoDB and Next-Auth.. not so sure what the issue is in this case. In my .env.local file I only have these two variables:

NEXTAUTH_SECRET="MUNKNATION"
NEXTAUTH_URL=http://localhost:3000

How I am pulling the data on local host:

export const getServerSideProps = async (context) => {
  const session = await getSession({ req: context.req });

  if (!session) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
  } else {
    let filePath = path.join(process.cwd(), "public", "data", "data.json");

    let jsonData = await fs.readFile(filePath);

    const data = JSON.parse(jsonData);

    const inProgressStatusData = data.productRequests.filter(
      (item) => item.status == "in-progress"
    );

    const liveStatusData = data.productRequests.filter(
      (item) => item.status == "live"
    );

    const plannedStatusData = data.productRequests.filter(
      (item) => item.status == "planned"
    );

    let filterData = filteredData(data, "suggestion");

    let feedbackData = {
      suggestions: filterData,
      progress: inProgressStatusData,
      planned: plannedStatusData,
      live: liveStatusData,
    };

    return {
      props: { session, feedbackData },
    };
  }
};


Folder structure:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2025-02-02 11:38:35

解决此问题的一个简单解决方案是,在您的getServersideProps中,而不是调用readfile使用readfilesync如下:

export const getServerSideProps = async (context) => {
...
    const file = readFileSync(
      join(process.cwd(), "public", "data", "data.json"),
      "utf8"
    );
    const data = JSON.parse(fileData);
...

我已经测试了此解决方案使用Vercel,它在开发和生产模式下正常工作。

A simple solution to this problem would be to, inside of your getServerSideProps, instead of calling readFile use readFileSync as follows:

export const getServerSideProps = async (context) => {
...
    const file = readFileSync(
      join(process.cwd(), "public", "data", "data.json"),
      "utf8"
    );
    const data = JSON.parse(fileData);
...

I have tested this solution with Vercel and it works correctly, in development and production mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文