简体   繁体   English

FetchError:请求 http://localhost:1337/api/products 失败,原因:连接 ECONNREFUSED 127.0.0.1:1337

[英]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!虽然这可能无法完全解决问题,但鉴于缺乏更多细节,这两个建议当然应该是一个好的开始,让我们继续解决这个问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FetchError:请求http:// localhost:3000 / api / projects失败,原因:connect ECONNREFUSED 127.0.0.1:3000(Express / Next.Js) - FetchError: request to http://localhost:3000/api/projects failed, reason: connect ECONNREFUSED 127.0.0.1:3000 (Express/Next.Js) npm ERR: FetchError: request to http.//registry.npmjs,org/webpack-dev-server failed: 原因。 连接 ECONNREFUSED 127.0.0:1:8000 - npm ERR! FetchError: request to http://registry.npmjs.org/webpack-dev-server failed, reason: connect ECONNREFUSED 127.0.0.1:8000 使用ParseServer快速连接 - 无法连接 <ip> 端口1337,连接超时 - Express with ParseServer - Failed to connect to <ip> port 1337, connection timeout 无法获取适配器:连接ECONNREFUSED 127.0.0.1:9080 - Failed to get adapter: connect ECONNREFUSED 127.0.0.1:9080 Redis 连接到 127.0.0.1:6379 失败 - 连接 ECONNREFUSED - Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED Redis与127.0.0.1:6379的连接失败-连接ECONNREFUSED 127.0.0.1:6379 Node.js - Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 nodejs 启动节点应用程序并访问localhost:1337后出现错误404 - Error 404 after starting the node application and visiting localhost:1337 正则表达式元音到 1337 个数字 - regex Vowels to 1337 Numbers 错误网络驱动程序:请求因错误而失败:在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14) 连接 ECONNREFUSED 127.0.0.1:4444 - ERROR webdriver: Request failed due to Error: connect ECONNREFUSED 127.0.0.1:4444 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14) [SequelizeConnectionRefusedError]:连接ECONNREFUSED 127.0.0.1:3306 - [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM