简体   繁体   English

如何在本机反应中访问 api 调用响应中的特定数据

[英]how to access particular data in api call response in react native

I am using react native and working with the moviesDB API.For some reason, I cannot access the data I am looking for in the response of the api call.我正在使用 react native 并使用 moviesDB API。由于某种原因,我无法访问我在 api 调用的响应中寻找的数据。 I am trying to get the "poster_path" info from the api call.我正在尝试从 api 调用中获取“poster_path”信息。 So in my console.log, If call this.state.movies, I see the data of many movies there as well as the "poster_path" key that I want to access and its info.因此,在我的 console.log 中,如果调用 this.state.movies,我会在那里看到许多电影的数据以及我想要访问的“poster_path”键及其信息。 However, when I console.log this.state.movies.poster_path, it shows undefined.但是,当我 console.log this.state.movies.poster_path 时,它显示未定义。 Just wondering why its doing that.只是想知道它为什么这样做。 I have tried searching online for answers and also tried to wrap brackets and take out the spread operator.我曾尝试在线搜索答案,并尝试将括号括起来并取出扩展运算符。 Please see below for code and picture of console.请参阅下面的控制台代码和图片。 Thanks!谢谢!

import { View, Text, StyleSheet, TextInput, Image } from "react-native";
import { FlatList, TouchableOpacity } from "react-native-gesture-handler";
import MovieItem from "../components/MovieItem";

const API_KEY2="*******";

class SearchScreen extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            movies: \[\],
            searchTerm: "",
        }
    }

    handleSubmit =(e)=> {

        fetch(`https://api.themoviedb.org/3/search/movie?api_key=${API_KEY2}&query=${this.state.searchTerm}`)
            .then(data => data.json())
            .then(data=> {
                this.setState({
                    movies: \[...data.results\]
                });
                console.log("RESPONSE FROM THIS.STATE.MOVIES", this.state.movies)
                console.log("RESPONSE FROM THIS.STATE.MOVIES.POSTER_PATH",this.state.movies.poster_path)

            }) 

    }

    handleChange=(textToSearch)=> {
        this.setState({
            searchTerm: textToSearch
        });
    }

    render() {


        return(
            <View style={styles.screen}>
                <TextInput 
                    style={styles.input}
                    onSubmitEditing={this.handleSubmit} 
                    onChangeText={(text)=>this.handleChange(text)} 
                    placeholder="Enter Movie" 
                    />
                    <FlatList 
                        data={this.state.movies} 
                        renderItem={({item})=> {
                            return(
                                <TouchableOpacity onPress={()=> this.props.navigation.navigate("MovieItem", {item})}>
                                    <View style={styles.movieItem}>
                                    <Image source={{uri:`https://image.tmdb.org/t/p/w1280/${item.poster_path}`}} 
                                        style={{
                                            height: 220, 
                                            width: 200
                                        }}/>
                                            <MovieItem item={item}/>
                                    </View>
                                </TouchableOpacity>
                                )
                    }} />
            </View>
        )
    }
}


const styles = StyleSheet.create({
    screen: {
        flex: 1,
        backgroundColor:"tomato",
        justifyContent:"center",
        alignItems:"center",
        flexDirection:"column"
    },
    input: {
        borderStyle:"solid", 
        borderWidth: 5, 
        width:"100%", 
        padding: 20,
        backgroundColor:"white",
        fontFamily:"Yesteryear-Regular",
        fontSize: 20,
        color:"tomato"
    },
    movieItem: {
        marginTop: 20,
        marginBottom: 20
    },

})


export default SearchScreen;][1]][1]

在此处输入图像描述

// response for this.state.movies // response for this.state.movies.poster_path // this.state.movies 的响应 // this.state.movies.poster_path 的响应

在此处输入图像描述

var temp = [{'poster_path':'xxx'}]; var temp = [{'poster_path':'xxx'}];

temp[0].poster_path; temp[0].poster_path;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本机反应的特定响应上调用 function - how To call function on particular response in react native 如何在 React Native 中打印此 API 响应数据? - How to print this API response data in React Native? 请求完成时,如何在 React js 中访问 api 调用的响应数据? - How can I access response data of api call in react js while request is completing? React Native:如何从 Api 调用中保存数据 - React Native: How save data from Api call API成功获取数据后如何在React Native中调用render - How to call render in React Native after API successfully fetches data 如何在React Native中使用Flatlist呈现响应API? - How to render the response API with Flatlist in react native? React.JS,如何使用来自第二个 API 调用的数据编辑第一个 API 调用的响应? - React.JS, how to edit the response of a first API call with data from a second API call? React Native:提取api调用返回不适当的响应 - React Native: Fetch api call is returning inappropriate response 如何从axios API调用获取响应的ID —使用React Native的Axios - how to get the id of a response from an axios API call — Axios with React Native 如何使用反应钩子从第一个 api 调用结果中获取第二个 api 调用需要特定数据的序列 api 调用? - How to fetch sequence api calls where second api call need particular data from first api call result using react hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM