未定义不是对象(评估' this.fetchData()。然后')

时间:2017-07-25 15:15:27

标签: react-native

本机函数在主渲染中绘制场景。但我得到错误作为这个问题的标题(未定义不是和对象......)

这是我的功能块:



 fetchData() {
        const { params } = this.props.navigation.state;           
        
        fetch(REQUEST_URL+'&'+'cabnum='+params.cabreg)
        .then((response) => response.json())
        .then((responseData) => {           
             //alert(params.cabreg);
             Alert.alert('','Your driver is :'+responseData[0].driver);
            return responseData;
  
            }).done();
        }




我的主渲染是:



render() {
            const { params } = this.props.navigation.state;
             
            return (
                <View>
                    <Text>{params.cabreg}</Text>
                    <View>{this.fetchData()
                            .then((responseData)=>{                                
                                    return(<View style={styles1.rightContainer}> 
                                            <Image source={{uri: responseData[0].image}} style={styles1.thumbnail} /> 
                                            <View style={{flexDirection:'column',alignItems:'center'}}>
                                                <Text style={styles1.title}>{responseData[0].reg}</Text>
                                                <Text style={styles1.driver}>Driver:{responseData[0].driver}</Text>
                                                <Text style={styles1.driver}>Cab Type:{responseData[0].ctype}</Text>
                                                <Text style={styles1.driver}>Contact:{responseData[0].contact}</Text>
                                            </View>
                                            </View>
                                            );
                                    }
                            )  
                        }
                    </View>
                </View>
            ); 
        }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这不是将响应数据呈现给视图的正确方法。

试试这个:

render()
{
const { params } = this.props.navigation.state

return (
    <View>
        <Text>
            {params.cabreg}
        </Text>
        <View>
            {this.fetchData()}
        </View>
    </View>
)
}

和这样的功能:

fetchData()
{
const { params } = this.props.navigation.state

fetch(REQUEST_URL + '&' + 'cabnum=' + params.cabreg)
    .then(response => response.json())
    .then(responseData => {
        //alert(params.cabreg);
        Alert.alert('', 'Your driver is :' + responseData[0].driver)
        return (
            <View style={styles1.rightContainer}>
                <Image
                    source={{ uri: responseData[0].image }}
                    style={styles1.thumbnail}
                />
                <View style={{ flexDirection: 'column', alignItems: 'center' }}>
                    <Text style={styles1.title}>
                        {responseData[0].reg}
                    </Text>
                    <Text style={styles1.driver}>
                        Driver:{responseData[0].driver}
                    </Text>
                    <Text style={styles1.driver}>
                        Cab Type:{responseData[0].ctype}
                    </Text>
                    <Text style={styles1.driver}>
                        Contact:{responseData[0].contact}
                    </Text>
                </View>
            </View>
        )
    })
    .done()
}