fetcherror:请求http:// localhost:1337/api/products失败,原因:连接econnrefuse 127.0.0.1:1337
我正在尝试使用Next.js和strapi建立一个电子商务网站。每当我尝试从strapi到Next.js的数据时,我总是会遇到错误: -
FetchError: request to http://localhost:1337/api/products?populate=* failed, reason: connect ECONNREFUSED 127.0.0.1:1337
?pupulate = *链接是接收所有数据,我也尝试了。
这就是我请求数据的方式: -
export async function getServerSideProps() {
let data = await fetch('http://localhost:1337/api/products?populate=*', {
method: 'GET',
headers: {
'Authorization': 'Bearer api-token',
},
});
let products = await data.json();
return {
props: { products },
};
}
我读了许多类似的问题,但找不到任何问题。我已经检查了很多次,但仍然没有工作。但是,当我使用 Thunder Client 使用相同的API令牌提出请求时,它给了我一个状态:200 ,我也会收到数据以JSON格式没有任何错误。 已经有几个小时了,一切看起来都不错,但仍然无法正常工作。
I am trying to build an e-commerce site with Next.JS and Strapi. Whenever I try to request data from Strapi to Next.JS, I always get error:-
FetchError: request to http://localhost:1337/api/products?populate=* failed, reason: connect ECONNREFUSED 127.0.0.1:1337
?populate=* in the link is to receive all data and I also tried without it.
This is how I am requesting data:-
export async function getServerSideProps() {
let data = await fetch('http://localhost:1337/api/products?populate=*', {
method: 'GET',
headers: {
'Authorization': 'Bearer api-token',
},
});
let products = await data.json();
return {
props: { products },
};
}
I have read many similar questions but can't find anything. I have checked everything many times but still not working. However, when I make the request with the same API token using thunder client, it gives me a status: 200, and I also receive data in JSON format without any error.
It's been hours and everything looks good but still not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将“ localhost”更改为“ 127.0.0.1”将其修复给我
参考:
strapi fetcherror:请求http:// localhost:1337/api/events失败,原因:Connect Econnrefused :: 1:1:1337
Changing "localhost" to "127.0.0.1" fixed it for me
reference:
strapi FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED ::1:1337
首先,当您从NextJS API获取时,您不会拨打完整的URL(即“ Local -Host”),您只需使用
/api/More/More/params 启动
调用首先值得阅读(很可能会回答您的问题) -api-routes“ rel =” nofollow noreferrer”>文档
getserversideprops
尽管这可能并不能完全解决问题,但鉴于当时缺乏更多细节,这两个建议肯定应该是一个良好的开始,并让我们开始解决这个问题!
First and foremost, when fetching from your nextjs api, you don't call the full url, (i.e., 'localhost'), you just start the call with
/api/more/params
I also think it’s first going to be worth reading (and will likely answer your question) the documentation on
getServerSideProps
While this may not entirely solve the problem, given then lack of further details, both these recommendations should certainly be a good start and get us goin on solving this!