Reputation: 43
I am trying to access the results (here it is the 'body') of the fetch but keep getting this :
Promise {< pending >} proto : Promise [[PromiseStatus]] : "resolved" [[PromiseValue]] : Array(100)
it says resolved but isnt just giving me the array I want.
here is my code
let Categories = fetch('http://localhost:3001/categories?' )
.then((response) => {
var data = response.json();
return data;
}).then((body) => {
console.log("fetching categories...")
console.log(body.categories)
return body.categories
})
console.log(Categories)
export default Categories
the above result comes from the console.log(Categories)
Upvotes: 1
Views: 4125
Reputation: 963
Categories
is still a Promise. You have to call .then
on it where you import it and you will receive your body.categories in the parameter.
EDIT: Additional info from comments (I can't format it nicely there).
It's async so it can't go to initialState of a Redux store. You should:
make an action creator that fetches this data
when the data is fetched dispatch an action with this data, e.g:
{ type: 'CATEGORIES_LOAD', data: data }
make a reducer which updates this store
And execute this action creator when your component mounts, e.g. componentWillMount if you also use React.
Upvotes: 2