I am returning an array from an async call in my action which then gets passed down to the reducer and finally back into the React. However, whenever I try to access the elements inside I get an error. My first guess was maybe my async call is wrong so I console.log everywhere. But everything seems fine except when I try to map over the array.
Here is the sequence of steps:
Dispatch Action:
.then(feeds => {
console.log('Sending data to dispatch');
console.log(`Testing this function -> ${JSON.stringify(feeds)}`);
dispatch({
type: 'RETRIEVE_FEEDS',
payload: feeds,
});
Initially feeds is an empty array in my reducer which then gets populated with this array.
Reducer:
case 'RETRIEVE_FEEDS': {
return { ...state, feeds: action.payload };
}
Now in my mapStateToProps I receive the initial empty array and then the populated array from dispatch.
const mapStateToProps = ({ feedState }) => {
const { feeds } = feedState;
console.log(`FeedState -> ${JSON.stringify(feedState.feeds)}`);
console.log(`Is Array -> ${Array.isArray(feedState.feeds)}`);
console.log(`Going to map through the array`);
feedState.feeds.map(feed =>{
console.log(`Feed -> ${JSON.stringify(feed)}`)
console.log(`Feed ID -> ${feed.feedID}`)
});
return { feeds };
};
My only issue is that whenever I try to get something from the array it gets undefined.
These are my logs:
FeedState -> []
Is Array -> true
Going to map through the array
Sending data to dispatch
Testing this function -> [[{"feedID":"57dfnQuwUghupbRB7EEB","uploadedBy":"8Vmr0ZnVDPfgkCqSBWHXjaVEDYH3","videoURL":"","datePosted":"2017-12-08T14:24:37.323Z","tags":[],"isLike":false,"likes":{"countLikes":0}}],[{"feedID":"reYEcurssCV32WyQgOYp","uploadedBy":"8Vmr0ZnVDPfgkCqSBWHXjaVEDYH3","videoURL":"","datePosted":"2017-12-08T14:46:13.655Z","tags":[],"isLike":false,"likes":{"countLikes":0}}]]
FeedState -> [[{"feedID":"57dfnQuwUghupbRB7EEB","uploadedBy":"8Vmr0ZnVDPfgkCqSBWHXjaVEDYH3","videoURL":"","datePosted":"2017-12-08T14:24:37.323Z","tags":[],"isLike":false,"likes":{"countLikes":0}}],[{"feedID":"reYEcurssCV32WyQgOYp","uploadedBy":"8Vmr0ZnVDPfgkCqSBWHXjaVEDYH3","videoURL":"","datePosted":"2017-12-08T14:46:13.655Z","tags":[],"isLike":false,"likes":{"countLikes":0}}]]
Is Array -> true
Going to map through the array
Feed -> [{"feedID":"57dfnQuwUghupbRB7EEB","uploadedBy":"8Vmr0ZnVDPfgkCqSBWHXjaVEDYH3","videoURL":"","datePosted":"2017-12-08T14:24:37.323Z","tags":[],"isLike":false,"likes":{"countLikes":0}}]
Feed ID -> undefined
Feed ID -> ${JSON.stringify(feed).feedId})"?