【问题标题】:React Native retrieving API data source.uri should not be an empty stringReact Native 检索 API 数据 source.uri 不应为空字符串
【发布时间】: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.log url 时,它显示它正在将纽约添加到过滤器中,但是结果显示其他情况
  • 好吧,让我检查一下

标签: react-native axios zomato-api


【解决方案1】:

所以我遇到了 2 个问题,一个是卡组件中您没有正确提供 uri,它应该是 image={{uri:item.restaurant.thumb}},其次对于 newyork,您的实体 ID 必须是

要在“纽约市曼哈顿”中搜索“意大利”餐厅, 设置美食 = 55,entity_id = 94741 和 entity_type = zone

它根据 zomato 文档,所以请检查一下。并找到博览会链接:expo-snack

import React from 'react';
import { 
    View,
    Text,
    FlatList,
    StyleSheet,
    Button,
    TouchableHighlight,
    ActivityIndicator,
    } from 'react-native';
import { createAppContainer } from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { Card, Image } from 'react-native-elements';
import Constants from 'expo-constants';
import axios from 'axios';

export default class CategoryScreen extends React.Component {

  constructor(props){
    super(props);
        this.state={
      data : [],
      isVisible: true,
      city : '94741'
    }
  }
async componentDidMount() {
    let id = "3"
    let city = this.state.city
    let result;
    try {
      result = await axios.request({
        method: 'get',
        url: `https://developers.zomato.com/api/v2.1/search?entity_id=${city}&entity_type=zone&category=${id}`,
        headers: {
          'Content-Type': 'application/json',
          'user-key': "a31bd76da32396a27b6906bf0ca707a2",
        },
      })
    } catch (err) {
      err => console.log(err)
    }
    this.setState({
      isLoading: false,
      data: result.data.restaurants
    })
    console.log(result)
    console.log(data)
  }
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={()=> alert(item.restaurant.location.city)}>
         <Card image={{uri:item.restaurant.thumb}} style={styles.container}>
           <Text style={{color:'#000',fontWeight:'bold'}}>{item.restaurant.name} </Text>
         </Card>
         </TouchableHighlight>
                  }
                />
            )
        }
      </View>
    );
  }

};

const styles = StyleSheet.create({

});

【讨论】:

  • 非常感谢!奇怪的是,如果我想选择像圣地亚哥这样的城市怎么办?我将如何检索该城市的 entity_id?
  • 我对 Zomato 的文档了解不多,但它需要在某个地方。只需检查一次
  • 我确信在某个地方有答案,互联网是一个很大的地方哈哈。谢谢你帮助我
  • 是的。奇怪但有用的地方
猜你喜欢
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2017-01-01
  • 2011-04-05
相关资源
最近更新 更多