连接strapi和next.js,但遇到错误
打开了两个终端,一个用于Strapi,一个用于Next.js。 我已经在Strapi中设置了配置。 我想在Next.js中接收API并将其显示在屏幕上,但并未显示。然后,我得到一个错误屏幕。 我与Postman进行了测试,并确认可以完成并邮寄。
我在浏览器中遇到了这个错误。
Server Error
Error: connect ECONNREFUSED ::1:1337
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown> (Error: connect ECONNREFUSED
: (1:1337)
TCPConnectWrap.afterConnect [as oncomplete]
node:net (1237:16)
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
wait - compiling...
event - compiled client and server successfully in 393 ms (125 modules)
wait - compiling / (client and server)...
wait - compiling...
event - compiled client and server successfully in 128 ms (144 modules)
wait - compiling /_error (client and server)...
wait - compiling...
event - compiled client and server successfully in 56 ms (145 modules)
error - Error: connect ECONNREFUSED ::1:1337
index.js
import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";
import axios from "axios";
export async function getStaticProps() {
const foodsData = await (
await axios.get("http://localhost:1337/api/foods")
).data.data;
console.log(foodsData);
return {
props: {
foods: foodsData,
},
};
}
export default function Home({ foods }) {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>Welcome to My Shopping</h1>
<div className={styles.grid}>
{foods.map((food, index) => (
<>
<a
href="https://nextjs.org/docs"
className={styles.card}
key={index}
>
<h2>{food.attributes.title}</h2>
<p>{food.attributes.description}</p>
<div>価格:{food.attributes.price}</div>
</a>
</>
))}
</div>
</main>
</div>
);
}
如何纠正? 帮我Plese。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。 http:// localhost:1337 to http://127.0.0.0.0.1.1:
更改
1337 127.0.0.1通常是可以互换的,并给出相同的结果,但是我们不能说它们完全是同义词。
您可以在此处阅读有关两者的更多信息:
ps:感谢我的同事和朋友Simen Daehlin在Strapi
I had this same issue. Change http://localhost:1337 to http://127.0.0.1:1337
Modified
The reason is that localhost and 127.0.0.1 are often interchangeable and give the same result, but we cannot say they are entirely synonymous.
You can read more about the two here: https://phoenixnap.com/kb/localhost-vs-127-0-0-1
PS: Thanks to my colleague and friend Simen Daehlin at Strapi ????????.