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

发布于 2025-01-30 18:04:44 字数 822 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2025-02-06 18:04:44

首先,当您从NextJS API获取时,您不会拨打完整的URL(即“ Local -Host”),您只需使用/api/More/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 },
 };
}

调用首先值得阅读(很可能会回答您的问题) -api-routes“ rel =” nofollow noreferrer”>文档 getserversideprops

当您想从服务器获取数据时,可以访问API路由,然后从GetServersideProps调用API路由。这是一种不必要且效率低下的方法,因为这将导致由于在服务器上运行的geterversideprops和API路由而引起额外的请求。

尽管这可能并不能完全解决问题,但鉴于当时缺乏更多细节,这两个建议肯定应该是一个良好的开始,并让我们开始解决这个问题!

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

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

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. 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.

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文