2017-02-20 139 views
1

我已經使用取API調用來訪問JSON數據和事實證明,訪問該數據,我在數據源已經設置它返回當陣營母語:訪問JSON數據

警告[ TypeError:undefined不是一個對象(評估'response.data.onwardflights')]

我一定會正確地提取數據,因爲我已經通過記錄數據看到了數據。

我也附上相同的源代碼!請幫我出去!

'use strict'; 

import React, {Component} from 'react'; 
import {StyleSheet, View, Text, ListView, ScrollView} from 'react-native'; 

const ds = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}); 

class BusList extends Component { 

    constructor() { 
    super(); 
    // const ds = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}); 
    this.state = { 
     dataSource: ds.cloneWithRows([]), 
     getData: [] 
    }; 
    } 

    componentDidMount() { 
     this.loadJSONData(); 
    } 

    loadJSONData() { 
     fetch('api') 
      .then((response) => { 
      this.setState({getData: response}) 
      this.setState({dataSource: ds.cloneWithRows(response.data.onwardflights)}) 
      return response.json() 
      }) 
      .then((responseJSON) => { 
        console.log(responseJSON) 
       return this.setState({dataSource: this.state.dataSource.cloneWithRows(responseJSON)}); 
      }) 
      .catch((error) => { 
      console.warn(error); 
      }); 
    } 

    renderRow(rowData) { 
     return (
      <View> 
       <Text>{rowData.origin}</Text> 
      </View> 
     ); 
    } 

    render() { 
    return (
     <View> 
      <ListView 
       dataSource = {this.state.dataSource} 
       enableEmptySections = {true} 
       renderRow = {(rowData) => <Text>{rowData}</Text>} 
      /> 
     </View> 
    ); 
    } 
} 

module.exports = BusList; 
+0

你可以發佈實際的JSON嗎? –

+0

@Pekka j json響應非常巨大! 任何替代方法將其發佈在評論本身? –

+0

然後只是相關位。 ''response.data.onwardflights'' –

回答

1

當然的response.data.onwardflightsundefined,因爲你沒有調用之前的response.json()來讀取數據。首先調用該方法,然後纔讀取數據。請閱讀this article