Cannot fetch data for a component from an API
Hello,
I want to fetch data that I successfully collect from a mongodb database and then display it in a dropdown component. But I am unable to receive it and receive
undefined
instead.
This is my API, a [...param].js
in folder api/calcCart
:
This is the function where I fetch this usersArray
. It is in index.js
:
This is the component where this function is called, also defined in index.js
:
I keep seeing this:
If it is being resolved successfully, why is there undefined?
If someone could kindly guide me. I have been on this for almost two weeks. Thank you.2 Replies
Can you hit your api successfully using postman or another application like that? Is this meant to be a POST without any data?
I feel like you’re also probably consuming the promise in the first “then”. If you’re expecting a JSON response, then you need to return response.json()
I've never used Postman but I managed to get it to work using
await
on response.json().
then(async (response) => {
const data = await response.json();
console.log("SUCCESS in Fetching Product Data", Object.values(data))}