2017-02-20 144 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