【发布时间】:2020-04-17 20:41:51
【问题描述】:
我正在尝试从 API (https://developers.zomato.com/documentation) 检索数据并获取餐厅名称和图像。但是,当我尝试加载图像时,会收到警告 source.uri should not be an empty string。
这是我的代码:
async componentDidMount() {
let id = this.props.navigation.state.params.category
let result;
try {
result = await axios.request({
method: 'get',
url: `https://developers.zomato.com/api/v2.1/search?category=${id}`,
headers: {
'Content-Type': 'application/json',
'user-key': "a31bd76da32396a27b6906bf0ca707a2",
},
})
} catch (err) {
err => console.log(err)
}
this.setState({
isLoading: false,
data: result.data.restaurants
})
}
render() {
return (
<View>
{
this.state.isLoading ?
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator style={{color:'red'}} />
</View> :
(
this.state.data.length == 0 ?
<View style={{ flex: 1, padding: 20 }}>
<Text style={{ color: '#000', fontWeight: 'bold' }}>No restaurants from selected category</Text>
</View> :
<FlatList
style={{ marginBottom: 80 }}
keyExtractor={item => item.id}
data={this.state.data}
renderItem={({ item }) =>
<TouchableHighlight onPress={()=> console.log(item.restaurant.thumb)}>
<Card image={item.restaurant.thumb} style={styles.container}>
<Image resizeMode='contain' source={{ uri: item.restaurant.thumb }}/>
<Text style={{color:'#000',fontWeight:'bold'}}>{item.restaurant.name} </Text>
</Card>
</TouchableHighlight>
}
/>
)
}
</View>
);
}
正如您所见,当我触摸任何一张卡片时,我正在控制台记录图像 uri 的链接,它完美地显示出来。为什么当应用程序加载图像时它们是空字符串,而当我通过控制台日志加载它时,链接却完美显示?
我正在使用 axios 加载我的 API
这里是世博小吃链接:https://snack.expo.io/r1XTaw4JU
【问题讨论】:
-
你能把这段代码分享在一个工作博览会小吃中,以便我检查
-
@GauravRoy 我已经更新了这个问题。另外,我正在尝试为纽约市添加一个参数,但是由于某种原因,结果仅显示来自新德里班加罗尔新加坡班加罗尔孟买等的餐厅。如果可以的话,请您提供一些输入,请?出于某种原因,当我
console.logurl 时,它显示它正在将纽约添加到过滤器中,但是结果显示其他情况 -
好吧,让我检查一下
标签: react-native axios zomato-api