简体   繁体   English

无法访问JavaScript对象数组中的属性(React.js)

[英]Can not access properties in JavaScript object array (React.js)

I have a React.js component which pulls its initial state data from an API call in componentDidMount(). 我有一个React.js组件,它从componentDidMount()中的API调用中提取其初始状态数据。 The data is an array of objects. 数据是一个对象数组。

I am able to view the array, and individual elements using JSON.stringify (for debugging), but when I try to access a property in an element, I get an error which seems to imply that the element is undefined, despite having checked that it is not. 我能够使用JSON.stringify(用于调试)查看数组和单个元素,但是当我尝试访问元素中的属性时,我得到一个错误,这似乎意味着该元素未定义,尽管已经检查过它不是。

Code: 码:

class TubeStatus extends Component {
  constructor(props) {
    super(props);
    this.state = { 'tubedata' : [] };
  };
  componentWillMount() {
    let component = this;
    axios.get('https://api.tfl.gov.uk/line/mode/tube/status')
    .then( function(response) {
      component.setState({ 'tubedata' : response.data });
    })
    .catch( function(error) {
      console.log(JSON.stringify(error, null, 2));
    });
  };
  render() {
    return (
      <div><pre>{this.state.tubedata[0].id}</pre></div>
    );
  }
}

Error: 错误:

Uncaught TypeError: Cannot read property 'id' of undefined

If I use JSON.stringify() to display this.state.tubedata, all the data is there. 如果我使用JSON.stringify()来显示this.state.tubedata,那么所有数据都在那里。

In my admittedly limited knowledge of React.js, I suspect this is because React.js is trying to access the .id property before componentDidMount() fires loading the initial state data, thus returning undefined, but I could be completely wrong. 在我对React.js知之甚少的知识中,我怀疑这是因为React.js试图在componentDidMount()触发加载初始状态数据之前访问.id属性,因此返回undefined,但我可能完全错了。

Anyone able to point me in the right direction? 有人能指出我正确的方向吗?

As you are fetching data from API call, on initial rendering data is not available hence you are getting error. 当您从API调用中获取数据时,初始渲染数据不可用,因此您将收到错误。

this.state.tubedata.length>0// check if tubedata is available 

So, 所以,

 render() {
    return (
      <div><pre>{this.state.tubedata.length>0?this.state.tubedata[0].id:null}</pre></div>
    );
  }

this is because you are having a async request and since the state array is initially empty this.state.tubedata[0] is initially undefined 这是因为你有一个异步请求,因为状态数组最初是空的, this.state.tubedata[0]最初是未定义的

Keep a check before using id like 在使用id之前先检查一下

<div><pre>{this.state.tubedata.length > 0 && this.state.tubedata[0].id}</pre></div>

 class TubeStatus extends React.Component { constructor(props) { super(props); this.state = { 'tubedata' : [] }; }; componentWillMount() { let component = this; }; render() { return ( <div>Hello <pre>{this.state.tubedata.length > 0 && this.state.tubedata[0].id}</pre></div> ); } } ReactDOM.render(<TubeStatus/>, document.getElementById('app')); 
 <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> <div id="app"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM