#Is strapi url correct? `/products/${id}?populate=*`
31 messages · Page 1 of 1 (latest)
I can only assume you got the URL wrong.
if your client is not served from port 1337 on localhost, it will fail. if the API server is expecting "api" at the beginning of the path, it will fail.
that is not the broken part
that sentence is meaningless
what is shown in the address bar when you're at the page where this request is being made?
if you're claiming these are the same server, you're missing /api at the beginning of the second fetch.
show the working one
wait what
5173 is not the same port
show the working fetch
if that works, I'm at a loss
log id before fetching
align with the params and the address bar
if it returns an empty array, strapi thinks it doesn't exist I suppose. I don't know strapi but the URL looks correct to me.
Show code and the result
You don’t return anything. You only log it…
again, you aren't returning anything from your function
how do you expect data to be anything other than undefined if you never return from fetchData()?
P.S. you're also mising await
return something and use await?
async function fetchData() {
// const id = useParams().id;
// console.log(id)
const response = await fetch('http://localhost:1337/api/products/1?populate=*');
const data = await response.json();
console.log(data.data.attributes.img.data.attributes.url);
return data;
}
const data = await fetchData()
...
you gotta learn how to debug things on your own
console.log(data) you'll see that there is no property named data
(would love to know why you said this worked before when it clearly didn't)
and yes, of course the console.log() works without await.. that's not what await is doing.
what is shown when you console.log(data) (inside fetchData())?
...
... helpful response