React Native 从 Zomato API 获取 collections

React Native getting collections from Zomato API

我正在尝试从 Zomato API (https://developers.zomato.com/documentation) 获取 collections,我正在尝试检索 collections 列表并将它们显示到 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 获得了 collections 数组中所有组件的完整列表。但是,当我尝试访问标题组件时;终端输出 undefined

我做错了什么?

Axios returns 承诺尝试将 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}))

请检查下面的代码,我认为你的代码有一个小问题,你没有提取准确的数据。我通过显示餐厅的标题来更正它。你可以做更多。世博会 link 就像 expo-link

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>
    );
  }
}

如有任何疑问请回复,我将清除它。希望对你有帮助;