[英]FetchError: request to http://localhost:1337/api/products failed, reason: connect ECONNREFUSED 127.0.0.1:1337
I am trying to build an e-commerce site with Next.JS and Strapi.我正在尝试使用 Next.JS 和 Strapi 构建一个电子商务网站。 Whenever I try to request data from Strapi to Next.JS, I always get error:-每当我尝试从 Strapi 向 Next.JS 请求数据时,我总是会收到错误消息:-
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.链接中的?populate= * 是接收所有数据,我也尝试过没有它。
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.但是,当我使用相同的 API 令牌使用thunder client发出请求时,它给了我一个状态:200 ,并且我还收到了 JSON 格式的数据,没有任何错误。 It's been hours and everything looks good but still not working.几个小时过去了,一切看起来都不错,但仍然无法正常工作。
First and foremost, when fetching from your nextjs api, you don't call the full url, (ie, 'localhost'), you just start the call with /api/more/params
首先,当从 nextjs api 获取时,你不会调用完整的 url,(即'localhost'),你只需使用/api/more/params
开始调用
export async function getServerSideProps() {
// next api routes use a proxy under the hood,
// so you just need to call `/api/` then the rest of the params :)
let data = await fetch('/api/products?populate=*', {
method: 'GET',
headers: {
'Authorization': 'Bearer api-token',
},
});
let products = await data.json();
return {
props: { products },
};
}
I also think it's first going to be worth reading (and will likely answer your question) the documentation on getServerSideProps
我还认为首先值得阅读(并且可能会回答您的问题) getServerSideProps
上的文档
It can be tempting to reach for an API Route when you want to fetch data from the server, then call that API route from getServerSideProps.当您想从服务器获取数据时,可能很容易使用 API 路由,然后从 getServerSideProps 调用该 API 路由。 This is an unnecessary and inefficient approach, as it will cause an extra request to be made due to both getServerSideProps and API Routes running on the server.这是一种不必要且低效的方法,因为它会由于服务器上运行的 getServerSideProps 和 API Routes 而导致发出额外的请求。
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!虽然这可能无法完全解决问题,但鉴于缺乏更多细节,这两个建议当然应该是一个好的开始,让我们继续解决这个问题!
Changing "localhost" to "127.0.0.1" fixed it for me将“localhost”更改为“127.0.0.1”为我修复了它
reference:参考:
strapi FetchError: request to http://localhost:1337/api/events failed, reason: connect ECONNREFUSED::1:1337 strapi FetchError:请求 http://localhost:1337/api/events 失败,原因:连接 ECONNREFUSED::1:1337
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.