我在列出用户的所有详细信息时遇到问题,我不明白为什么错误显示我的映射未定义不是函数。我创建了一个函数,它在函数内部是用户列表,所以在我列出用户之后,我把它放在渲染上,但是映射显示未定义的函数..
我将向你们展示我已经制作的示例代码:
构造函数:
constructor(props) {
super(props);
this.state = {
driver_id:'',
driver_history_list:[]
}
this.renderCardHistoryList = this.renderCardHistoryList.bind(this);
}
山:
async componentDidMount() {
NavigationBar.setColor('white')
NavigationBar.setStatusBarColor('#008E9B',true)
NavigationBar.setStatusBarTheme('white',true)
const ids_driver = await AsyncStorage.getItem('IDS');
axios.get('http://192.168.100.111:8001/api/driver_history/'+ids_driver)
.then((response) => {
this.setState({
driver_history_list:response
})
}).catch(function(error) {
alert(JSON.stringify(error));
})
}
功能:
renderCardHistoryList() {
const history_list = Array.isArray(this.state.driver_history_list) && this.state.driver_history_list || [];
return history_list.map(driver_info => {
return (
<Card>
<CardItem header button onPress={() => alert("This is Card Header")}>
<Left style={{flex:1}}>
</Left>
<Right>
<Text style={{fontSize:15}}>12 May 2019, 08:46pm</Text>
</Right>
</CardItem>
<CardItem button onPress={() => alert("This is Card Body")}>
<Body style={{bottom:'6%',position:'relative'}}>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}>
<Icon type="MaterialCommunityIcons" name='store' style={{fontSize:20,color:'#0a60ff'}}/>
<Text style={{marginLeft:'5%',fontSize:14}}>Store 1790</Text>
</View>
<Icon type="Entypo" name='dots-two-vertical' style={{fontSize:12,marginLeft:'1%',color:'grey',marginTop:'1%'}}/>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center',marginTop:'1%' }}>
<Icon type="MaterialCommunityIcons" name='map-marker-radius' style={{fontSize:20,color: "#f92223"}} />
<Text style={{marginLeft:'5%',fontSize:14}}></Text>
</View>
<Icon type="Entypo" name='dots-two-vertical' style={{fontSize:12,marginLeft:'1%',color:'grey',marginTop:'1%'}}/>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center',marginTop:'1%' }}>
<Icon type="Ionicons" name='ios-checkmark-circle' style={{fontSize:20,color: "#FF9501"}} />
<Text style={{marginLeft:'5%',fontSize:14}}></Text>
</View>
</Body>
</CardItem>
</Card>
)
})
}
使成为:
render() {
return (
<Container>
<Header style={{backgroundColor:'#008E9B'}}>
<Left style={{flex:1}}>
<Button transparent>
<Icon name='arrow-back' />
</Button>
</Left>
<Body style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Title>History</Title>
</Body>
<Right style={{flex: 1}} />
</Header>
<Content padder>
{this.renderCardHistoryList()}
</Content>
</Container>
)
}
结果:
验证您的阵列。 const history_list= this.state.driver_history_list || [];
renderCardHistoryList() {
const history_list = Array.isArray(this.state.driver_history_list) && this.state.driver_history_list || [];
return history_list.map(driver_info => {
return (
<Card>
<CardItem header button onPress={() => alert("This is Card Header")}>
<Left style={{flex:1}}>
</Left>
<Right>
<Text style={{fontSize:15}}>12 May 2019, 08:46pm</Text>
</Right>
</CardItem>
<CardItem button onPress={() => alert("This is Card Body")}>
<Body style={{bottom:'6%',position:'relative'}}>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}>
<Icon type="MaterialCommunityIcons" name='store' style={{fontSize:20,color:'#0a60ff'}}/>
<Text style={{marginLeft:'5%',fontSize:14}}>Store 1790</Text>
</View>
<Icon type="Entypo" name='dots-two-vertical' style={{fontSize:12,marginLeft:'1%',color:'grey',marginTop:'1%'}}/>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center',marginTop:'1%' }}>
<Icon type="MaterialCommunityIcons" name='map-marker-radius' style={{fontSize:20,color: "#f92223"}} />
<Text style={{marginLeft:'5%',fontSize:14}}></Text>
</View>
<Icon type="Entypo" name='dots-two-vertical' style={{fontSize:12,marginLeft:'1%',color:'grey',marginTop:'1%'}}/>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center',marginTop:'1%' }}>
<Icon type="Ionicons" name='ios-checkmark-circle' style={{fontSize:20,color: "#FF9501"}} />
<Text style={{marginLeft:'5%',fontSize:14}}></Text>
</View>
</Body>
</CardItem>
</Card>
)
})
}
编辑
您正在将整个响应设置为 driver_history_list,这是一个将其更改为的对象。
driver_history_list: response.data
笔记
始终验证您的数据。不要假设您将始终获得正确的数据。使用对象时,始终验证它们,这样做data.name
可能会破坏您的应用程序,如果data
为 null 或未定义。例如,给定以下对象。
const animal = {};
正在做
// throws an error, Cannot read property 'toLowerCase' of undefined
console.log(animal.name.toLowerCase())
为了防止这种情况发生,我们需要检查属性是否存在,如下所示。
// checks if the name property exists console name, else assign a console log 'Lion'
console.log(animal.name && animal.name.toLowerCase() || 'Lion')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句