0

我正在尝试从 Zomato API ( https://developers.zomato.com/documentation ) 获取集合,并且正在尝试检索集合列表并将它们显示到 flatList 上。但是,每次我尝试检索它时,我的终端似乎都会输出undefined

这是我的代码

async  componentDidMount(){
    try {
    const res = await axios.request({
      method: 'get',
      url: `https://developers.zomato.com/api/v2.1/collections`,
      headers: {
        'Content-Type': 'application/json',
        'user-key': 'a31bd76da32396a27b6906bf0ca707a2'
      },
      params: {
        'city_id': `${this.state.loca}`
      }
    });
    this.setState({ data: res.data });
    console.log(res.data.collections.title)
  } catch (err) {
    console.log(err);
  } finally {
    this.setState({ isLoading: false });
  }
 };

当我console.log(res.data.collections)从 API 获取集合数组中所有组件的完整列表时。但是,当我尝试访问标题组件时;终端输出undefined

我究竟做错了什么?

4

2 回答 2

1

请查看下面的代码,我认为您的代码有一个小问题,您没有提取确切的数据。我已经通过显示restuarent的标题来纠正它。你可以做更多。世博链接与世博链接一样

import React from 'react';
import { 
    View,
    Text,
    FlatList,
    StyleSheet,
    TouchableHighlight,
    Dimensions,
    Image,
    } from 'react-native';
import Modal from 'react-native-modal';
import { createAppContainer } from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';
import { Card, Icon, Button } from 'react-native-elements';
import Constants from 'expo-constants';
// import {apiCall} from '../src/api/Zomato';
// import Logo from '../assets/Logo.png';
import axios from 'axios';

export default class HomeScreen extends React.Component {

  constructor(props){
    super(props);
    // this.navigate = this.props.navigation.navigate;
    this.state={
      data : [],
      isModalVisible: false,
      loca: 280 
    }
  }

  async  componentDidMount(){
    try {
    const res = await axios.request({
      method: 'get',
      url: `https://developers.zomato.com/api/v2.1/collections`,
      headers: {
        'Content-Type': 'application/json',
        'user-key': 'a31bd76da32396a27b6906bf0ca707a2'
      },
      params: {
        'city_id': `${this.state.loca}`
      }
    });

    // alert(res.data.collections, 'response');
    this.setState({ data: res.data.collections });
  } catch (err) {
    console.log(err);
  } finally {

  }
 }
render() {
    return (
      <View>
         <FlatList 
         style={{marginBottom: 80}}
         keyExtractor={item => item.id}
         data={this.state.data}
         renderItem={({ item }) => 
         <TouchableHighlight onPress={()=> this.props.navigation.navigate('CategoryScreen', { category: item.categories.id, city: this.state.loca })}>
         <Card>
           <Text style={{color:'#000',fontWeight:'bold'}}>{item.collection.title} </Text>
         </Card>
         </TouchableHighlight>} 
      />

      </View>
    );
  }
}

如果有任何疑问,请回复,我会清除它。希望能帮助到你;

于 2020-01-04T09:54:02.320 回答
0

Axios 返回一个承诺尝试保持 setState.then并停止信任 console.log

axios.request({
  method: 'get',
  url: `https://developers.zomato.com/api/v2.1/collections`,
  headers: {
    'Content-Type': 'application/json',
    'user-key': 'a31bd76da32396a27b6906bf0ca707a2'
  },
  params: {
    'city_id': `${this.state.loca}`
  }
}).then( res => this.setState({res}))
于 2020-01-04T09:16:48.320 回答