[英]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.