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>
);
}
[Error]: connect ECONNREFUSED ::1:3000
Build Time Error on Console
at I.from (E:usman datamaincorenextjs.nextserverappdashboardadminpage.js:11:10985)
at y. (E:usman datamaincorenextjs.nextserverappdashboardadminpage.js:13:21276) {
port: 3000,
address: ‘::1’,
syscall: ‘connect’,
code: ‘ECONNREFUSED’,
errno: -4078,
method: ‘get’,
url: ‘http://localhost:3000/api/waitinglist/’,
data: undefined
},
.env file
NEXT_PUBLIC_WAITINGLIST_URL="http://localhost:3000/api"
versions
** Node ** : * 18.18.1 *
** Nextjs ** : * 14.2.1 *
2
Answers
Try renaming
NEXT_PUBLIC_WAITINGLIST_URL
toWAITINGLIST_URL
.If your env variables start with
NEXT_PUBLIC
prefix, it will be accessed by the browser not server and you’re in a server component.You can refer to https://nextjs.org/docs/app/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser
Looks like it has to do with ipv4 vs ipv6 resolution, answered previously here: https://stackoverflow.com/a/75842143/9985521