Nextjs 在服务器端组件获取数据期间连接 ECONNREFUSED ::1:3000

问题描述 投票:0回答:2
import React from "react";
import axios from "axios";

interface UsersType {
id: string;
firstName: string;
lastName: string;
email: string;
}
interface dataProps {
 allUsers: UsersType[];
}

 async function getData() {


   try {
    const { data } = await axios.get<dataProps>(
    `${process.env.NEXT_PUBLIC_WAITINGLIST_URL}/waitinglist/`
    );

      // console.log(data);

    return data;
   } catch (error) {
   console.log(error);
   }


      }

  export default async function Page() {
   const data = await getData();
   // console.log(data?.allUsers);

   return (
     <div className="text-white w-100">
     <table className="w-full table  ">
        <thead>
         <tr className=" [&>th]:text-center [&>th]:py-3 [&>th]:px-2 ">
           <th>First Name</th>
           <th>Last Name</th>
          <th>Email</th>
         </tr>
       </thead>
       <tbody>
          {data?.allUsers?.map((item: UsersType) => {
        return (
          <tr
            key={item?.id}
            className=" [&>td]:text-center [&>td]:py-3 [&>td]:px-2 "
          >
            <td>{item?.firstName}</td>
            <td>{item?.lastName}</td>
            <td>{item?.email}</td>
          </tr>
        );
      })}
    </tbody>
  </table>
</div>

); }

Build Time Error on Console

[错误]:连接 ECONNREFUSED ::1:3000 在 I.from (E:\usman data\maincorenextjs.next\server pp\dashboard dmin\page.js:11:10985) 在 y。 (E:\usman data\maincorenextjs.next\server pp\dashboard dmin\page.js:13:21276) { 端口:3000, 地址1', 系统调用:'连接', 代码:'ECONNREFUSED', 错误号:-4078,

方法:'获取', url: 'http://localhost:3000/api/waitinglist/', 数据:未定义 },

.env 文件

NEXT_PUBLIC_WAITINGLIST_URL="http://localhost:3000/api"

版本

** 节点 ** : * 18.18.1 * ** Nextjs ** : * 14.2.1 *

javascript reactjs node.js next.js fetch
2个回答
0
投票

尝试将

NEXT_PUBLIC_WAITINGLIST_URL
重命名为
WAITINGLIST_URL
。 如果您的环境变量以
NEXT_PUBLIC
前缀开头,它将由浏览器而不是服务器访问,并且您位于服务器组件中。

您可以参考https://nextjs.org/docs/app/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser


0
投票

看起来它与 ipv4 与 ipv6 分辨率有关,之前在这里回答过:https://stackoverflow.com/a/75842143/9985521

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