#astro.url.searchparams on component

3 messages · Page 1 of 1 (latest)

slim cairn
#

Im trying to pass an "id" before fetching the data, but when using Astro.url.searchParams.get('id') it returns null

my code:

---
const id = Astro.url.searchParams.get('id');
let data = [];

if (id) {
  const response = await fetch(`http://127.0.0.1:8000/faltas?id=${id}`);
  data = await response.json();
}
---
rugged orbitBOT
#

Hi @slim cairn. 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 provided knowledge sources, it seems that you might be trying to use a deprecated feature of Astro. In Astro v3.0, the Astro.request.params API was deprecated and it was removed entirely in Astro v4.0. The supported replacement is Astro.params.

Here's how you can use it:

---
const { id } = Astro.params;
let data = [];

if (id) {
  const response = await fetch(`<http://127.0.0.1:8000/faltas?id=${id}`>);
  data = await response.json();
}
---

Astro.params is an object containing the values of dynamic route segments matched for this request. In static builds, this will be the params returned by getStaticPaths() used for prerendering dynamic routes. In SSR builds, this can be any value matching the path segments in the dynamic route pattern.

For more information, you can refer to the Astro documentation (https://docs.astro.build/en/reference/api-reference/#astroparams).

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