[英]I am trying to map through array loaded from TMDB API
I uploaded my Data using FETCH from the TMDB API, When i try to map through it, it gives me an error undefined is not an object (evaluating 'NowMovies.map')我使用 TMDB API 中的 FETCH 上传了我的数据,当我尝试通过它访问 map 时,它给了我一个错误 undefined is not an object (evaluating 'Moviemap')
this is my code:这是我的代码:
constructor(props){
super(props);
this.state={
NowMovies:[],
isLoaded:false
}
}
componentDidMount(){
fetch('API')
.then(data => data.json())
.then(({MoviesLists}) => {this.setState({
isLoaded:true,
NowMovies:MoviesLists
})
})
}
render(){
var {isLoaded,NowMovies} = this.state
if (!isLoaded){
return <div> .... Loading</div>
}
else{
return(
<div>
<ul>
{NowMovies.map(Movie=>(<li key={NowMovies.id}> name:{NowMovies.original_title}</li>
))}
</ul>
</div>
this is what the API loades这就是 API 加载的内容
can anyone help me with loading this data to my array and map through it??谁能帮我将这些数据加载到我的阵列和 map 通过它?
You're trying to access NowMovies
within the map function, you should use the Movie
object you've passed instead.您正在尝试访问
NowMovies
中的 NowMovies,您应该改用您通过的Movie
object。
{NowMovies.map(Movie =>( <li key={Movie.id}> name:{Movie.original_title} </li> }
You can not assign directly the value, for that you can use.concat() or.push().您不能直接分配值,因为您可以使用.concat() 或.push()。 And correct with.map().
并用.map() 更正。
constructor(props){
super(props);
this.state={
NowMovies:[],
isLoaded:false
}
}
componentDidMount(){
fetch(API)
.then((data) => data.json())
.then(({MoviesLists}) => {
let movieList = this.state.NowMovies.concat(MoviesLists);
this.setState({
isLoaded:true,
NowMovies : movieList
})
})
}
render(){
var {isLoaded,NowMovies} = this.state
if (!isLoaded){
return <div> .... Loading</div>
}
else{
return(
<div>
<ul>
{NowMovies.map(Movie=>(<li key={Movie.id}> name:{Movie.original_title}</li>
))}
</ul>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.