javascript - 如何在本机反应中访问api调用响应中的特定数据
问题描述
我正在使用 react native 并使用 moviesDB API。由于某种原因,我无法访问我在 api 调用响应中寻找的数据。我正在尝试从 api 调用中获取“poster_path”信息。因此,在我的 console.log 中,如果调用 this.state.movies,我会在那里看到许多电影的数据以及我想要访问的“poster_path”键及其信息。但是,当我 console.log this.state.movies.poster_path 时,它显示未定义。只是想知道它为什么这样做。我曾尝试在线搜索答案,并尝试将括号括起来并取出扩展运算符。请参阅下面的控制台代码和图片。谢谢!
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]
// this.state.movies 的响应 // this.state.movies.poster_path 的响应
解决方案
var temp = [{'poster_path' :'xxx'}];
temp[0].poster_path;
推荐阅读
- java - 使用 maven Spigot 编译 mysql 依赖项
- flutter - Dart:解析 api 响应并从父级动态创建子 T
- amazon-web-services - 适用于 SaaS 应用程序的 AWS ECS
- mysql - 如何在 MySQL 中显示所有结果,不包括特定行中多个项目共享的结果?
- python - Python提取数据框中两个特殊字符之间的数字
- amazon-web-services - AWS CloudFormation 模板格式错误:未解决的资源依赖关系
在模板的资源块中 - reactjs - 如何告诉打字稿地图方法中的数组不为空?
- sql - 这是一个错误,还是 Snowflake 不完全支持 WHERE EXISTS 子句中的相关子查询?
- scala - 如何在“Scala”中为 Caffeine LoadingCache 编写缓存加载器以使“refreshAfterWrite”正常工作
- javascript - 如何用另一个更新一个对象的所有匹配键