#Fetch TypeError

1 messages · Page 1 of 1 (latest)

deep siren
#

I'm running a ASP.NET API in my localhost and have tested it with postman and swagger and it works fine but when i try to fetch it in astro it tells me TypeError

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11372:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (C:\Users\MSI\Workstation\pokemon-review-api\front-end\src\pages\index.astro:4:18)
    at async callComponentAsTemplateResultOrResponse (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/runtime/server/render/astro/render.js:85:25)
    at async renderToReadableStream (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/runtime/server/render/astro/render.js:35:26)
    at async renderPage (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/runtime/server/render/page.js:29:12)
    at async renderPage (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/core/render/core.js:50:20)
    at async #tryRenderRoute (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/core/pipeline.js:110:18)
    at async DevPipeline.renderRoute (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/core/pipeline.js:58:20)
    at async handleRoute (file:///C:/Users/MSI/Workstation/pokemon-review-api/front-end/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/astro/dist/vite-plugin-astro-server/route.js:248:18)
sterile oasisBOT
#

Hi @deep siren. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems like you're encountering a common issue with Astro's fetch function when trying to access local servers. The error TypeError: fetch failed is often caused by the fetch function being unable to connect to the local server.

A common solution to this problem, as mentioned in this GitHub issue (https://github.com/withastro/astro/issues/9249), is to replace "localhost" in your fetch URL with the IP address "127.0.0.1". This is because "localhost" might be resolved to the IPv6 address "::1" instead of the IPv4 address "127.0.0.1", and if your server is only listening on IPv4, it will refuse the connection.

So, if your fetch URL was something like <http://localhost:5000/api/posts,> you should change it to <http://127.0.0.1:5000/api/posts.>

If this doesn't solve your problem, it might be helpful to check if your local server is running properly and is accessible from other tools like Postman or your browser.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@deep siren kapa.ai is still learning and improving, please let us know how it did by reacting below

deep siren
#

it's still the same despite changing it to 127.0.0.1 :

---
import BaseLayout from '@/layouts/BaseLayout.astro'
import { buttonVariants } from '@/components/ui/button'
const response = await fetch('https://127.0.0.1:7169/api/Review');
---


<BaseLayout title="Pokemon Reviews" description="Pokemon Reviews">
  <section class="container grid items-center gap-6 pb-8 pt-6 md:py-10">



  </section>
</BaseLayout>