【发布时间】:2020-01-03 12:25:34
【问题描述】:
每次我的渲染运行地图循环时,我都试图运行函数getJarak()。我尝试了很多方法,但使用 async await 时仍然出现错误。
const getJarak = async (lat, lng) => {
const lats = this.state.lastLat;
const lngs = this.state.lastLong;
try {
const response = await axios.get('https://maps.googleapis.com/maps/api/distancematrix/json?origins=' + lats + ',' + lngs + '&destinations=' + lat + ',' + lng + '&key=APIKEY');
console.log(response.data.rows[0].elements[0].distance.text);
const data = response.data.rows[0].elements[0].distance.text
return data
} catch (error) {
console.log("error", error);
}
}
return this.state.healthCareQ.map((health, id) => {
return (
<TouchableOpacity key={id} activeOpacity={.6} onPress={() => {
this.props.navigation.navigate('HealthcareDisSubdis', { health });
}}>
<View style={stylesLocation.health} >
<View style={{ flex: 1, borderRadius: 14 }}>
<Image
style={stylesLocation.healthImage}
source={health.logo === "" || health.logo === null ? require('../../asset/noimage.png') : { uri: `${health.logo}` }}
/>
</View>
<View style={stylesLocation.healthDetails}>
<View style={{ flex: 1, flexDirection: 'column', justifyContent: 'center' }}>
<Text style={{ fontSize: 14, fontWeight: 'bold', width: 230, flexWrap: 'wrap' }}>
{health.hfc_name}
</Text>
<Text style={{ fontSize: 12, color: '#A5A5A5', paddingTop: 5, width: 215, flexWrap: 'wrap' }}>
{health.address1}
</Text>
</View>
</View>
<View style={{ flex: 0.90, justifyContent: 'center' }}>
{/* <Text style={{ fontWeight: 'bold' }}>{parseFloat(health.distance / 1000).toFixed(1)} KM</Text> */}
<Text style={{ fontWeight: 'bold' }}>{getJarak(health.latitude, health.longitude)}</Text>
</View>
</View>
</TouchableOpacity>
);
})
【问题讨论】:
-
欢迎来到 Stack Overflow。请不要上传images of code。它们不能被复制来重现问题,它们不能被未来的读者搜索,它们比文本更难阅读。请以文本形式发布实际代码以创建minimal reproducible example。
-
请检查您的值,您在代码中将对象显示为文本。
-
这能回答你的问题吗? Reactjs async rendering of components
-
@JigarShah ,我的值 (response.data.rows[0].elements[0].distance.text) 是字符串
-
请将错误信息的一部分以文本形式发布,以便以后的读者可以搜索到
标签: javascript reactjs react-native async-await