Trying to go through a simple Axios tutorial to start getting used to populating dynamic data. Loading and Using External Data in React
I had to create a JSON web server since hitting Codepen with localhost violated CORS.
App.jsx
var App = React.createClass({
getInitialState: function(){
return{
users: []
};
},
componentDidMount: function(){
var _this = this;
this.serverRequest =
axios
.get("http://localhost:3004/users")
.then(function(results){
console.log(results.data);
_this.setState({
users: results.data.users
});
});
},
componentWillUnmount: function(){
//...
},
render: function(){
return(
<div>
<h3>Returning Users:</h3>
{this.state.users.map(function(user) {
return (
<div>{user.name}</div>
);
})}
</div>
);
}
});
export default App;
I understand that data should be extrapolated out into something like Redux but this is just a start to working with dynamic data. The error I'm getting is Uncaught (in promise) TypeError: Cannot read property 'map' of undefined
I can't find very good explanations of this error doing some research. So what am I doing wrong?
I see that if I pre-populate data into the getInitialState
I can get a return. So what is going wrong with the Promise getting data loaded into the initial state?
Edits:
Console.log Image:
console.log(results.data);
– Abdennour TOUMIgetInitialState
– wsfullerconsole.log(results.data);
? – Mayank Shuklaresults.data
orresults.data.users
?? – Mayank Shukla