現在、Reactを使ってページを作っています。そのページ内ではAPIを叩いて、データを取得した後、その中の特定の1つを表示させようとしています。
今、こんな感じでDBの情報を this.state.users に入れています。
JavaScript
1constructor(props) { 2 super(props); 3 this.state = { 4 users: [], 5 }; 6 } 7 8 //初期化(画面レンダリング前) 9 componentWillMount(){ 10 this.fetchResponse(); 11 } 12 13 //リストの更新 14 fetchResponse() { 15 fetch('') 16 .then ((res) => res.json() ) 17 .then ((res) => { 18 this.setState({ 19 users: res, 20 }); 21 // console.log(res); 22 }) 23 }
その後、下記render()内でこのようにアクセスをしています。 2行目の console.log(this.state.users[99]);
では しっかりとすべての情報が表示されます。(プロパティ[“birthday”]もusers連想配列のkeyとして表示されます。)
しかしながら、3行目の console.log(this.state.users[99].birthday);
を動かすと、 cannot read property ‘birthday’ of undefined とエラーが出ます。
なぜかわかりますか?
JavaScript
1render() { 2 console.log(this.state.users[99]); 3 // console.log(this.state.users[99].birthday);
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/04 00:14
2018/09/04 00:41
2018/09/04 00:44