繁体   English   中英

错误:使用 Next.JS 和 serverless-mysql 在 Vercel 上获取地址信息 ENOTFOUND

[英]Error: getaddrinfo ENOTFOUND on Vercel with Next.JS and serverless-mysql

我正在使用 Next.js 并且在 API 方面我写了一个虚拟代码来使用这个库从我的数据库中获取行: serverless-mysql

我已经按照文档中的示例进行操作,并且在我的计算机上这工作得很好,我可以连接到数据库并获取行。 我的数据库在我的 VPS 上,而不是在我的本地主机上。

但是当我在 Vercel 上部署我的代码时,我尝试访问 /api/hello 在我的 Vercel 日志中我有这个错误:

[GET] /api/hello
{
  error: Error: Error: getaddrinfo ENOTFOUND "**.***.**.**"
      at connect (/var/task/node_modules/serverless-mysql/index.js:80:15)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)
      at async Object.query (/var/task/node_modules/serverless-mysql/index.js:182:5)
      at async excuteQuery (/var/task/.next/server/pages/api/hello.js:33:25)
      at async handler (/var/task/.next/server/pages/api/hello.js:59:24)
      at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils.js:102:9)
      at async Server.handleApiRequest (/var/task/node_modules/next/dist/server/next-server.js:1064:9)
      at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:951:37)
      at async Router.execute (/var/task/node_modules/next/dist/server/router.js:222:32)
      at async Server.run (/var/task/node_modules/next/dist/server/next-server.js:1135:29)
}

(我已经将消息中显示的真实Ip替换为“**.***.**.**”)

我的数据库接受来自外部的连接,因为我可以在我的计算机上访问它。

我还在项目设置中正确配置了 the.env var。

非常感谢您的帮助

您需要在 vercel 仪表板和 nextjs 应用程序上设置环境变量。

在你的 .env 文件中

 NEXT_PUBLIC_VERCEL_URL = "http://localhost:3000";

在您的代码中,引用变量

export const getBaseUrl = () => {
   if (process.env.NODE_ENV === "development") {
      return "http://localhost:3000";
    }

   return process.env.NEXT_PUBLIC_VERCEL_URL;
 }

然后,您可以在代码中的任何位置执行实用程序 function。

在 vercel 上,将环境变量设置为 VERCEL_URL 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM