尝试调用存储在反应状态的对象属性时出错。如果我将确切的roperty添加到一个状态它工作fint。但是,如果我将对象存储在我的状态并尝试调用该属性,我会收到错误消息:
Cannot read property '_embedded' of undefined
这是我的代码
var axios = require('axios');
var App = React.createClass({
getInitialState: function() {
return {
warningName: '',
warningBase: {}
},
}
},
componentDidMount: function() {
var _this = this;
var help = this.state.dietNames;
axios
.get("http://api.baggid.com/users/61113f093cc04421aa3900d53e929d6a/profiles/2850/warninggroups/")
.then(function(result) {
_this.setState({
warningName: result.data._embedded["bgd:warninggroup"][0]._embedded["bgd:warning"],
warningBase: result.data._embedded["bgd:warninggroup"]
});
})
render(){
console.log(this.state.warningName); //works
console.log(this.state.warningBase[0]._embedded["bgd:warning"]); //doesn't work
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>