#TypeScript problem with fetch...

1 messages · Page 1 of 1 (latest)

carmine coralBOT
#

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

#

The issue you're encountering is due to the way you're handling the fetch promise. In your current code, you're not returning anything from the .then() block, which is why you're getting a void type error when you try to use .map() on services.

Here's the problematic part of your code:

const services = await fetch(`<https://marctina-cms.up.railway.app/api/services/?populate=*`>)
.then(res => {res.json()})

You should return the result of res.json() like this:

const services = await fetch(`<https://marctina-cms.up.railway.app/api/services/?populate=*`>)
.then(res => res.json())

This way, services will be assigned the result of the res.json() promise, which should be the data you're trying to fetch. Then you should be able to use .map() on services.

Remember, in Astro, you can fetch data directly in the frontmatter of the Astro component using top-level await, as mentioned in the Astro documentation (https://docs.astro.build/en/guides/data-fetching/).

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