未处理的拒绝(TypeError):this.state.movie.map 不是一个函数
2018-06-06
2588
经过几个小时的搜索和尝试自己修正代码,我还是无法让它工作,也找不到关于我的具体问题的任何明确文档。任何帮助都将不胜感激!
我正在尝试制作一个电影评级应用程序,我几乎已经完成了。但是,我现在想做的是从 API 显示我的信息。我有一个单独的组件“SearchForm.tsx”,它处理在 API 调用后在 app.tsx 中调用的搜索字段,即“ + SearchForm ”,这应该等于 API 的查询。当我尝试输入文本并按回车键时,我卡在了:
“未处理的拒绝(TypeError):this.state.movie.map 不是一个函数”。
问题发生在我的 App.tsx 文件的第 46 行和第 27 行。
API 示例:
这是我尝试显示“ https://api.themoviedb.org/3/search/movie?api_key=58db259ec7d41d210fee1d1dc69b6fca&query=home "
App.tsx
import axios from "axios";
import * as React from "react";
import "./App.css";
import MoviesList from "./Components/MoviesList";
import SearchForm from "./Components/SearchForm";
export default class App extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.state = {
loading: true,
movie: []
};
this.handleSubmit = this.handleSubmit.bind(this);
}
public handleSubmit(value: any) {
// const searchInput = value; // you get the value of movieName input here
const sortByPop = "&sort_by=popularity.desc";
const requestUrl =
"https://api.themoviedb.org/3/search/movie?api_key=58db259ec7d41d210fee1d1dc69b6fca&query=" +
SearchForm +
sortByPop;
return axios.get(requestUrl).then(response => {
[27]err this.setState(
loading: false,
movie: response.data
});
// tslint:disable-next-line:no-console
console.log(response.data);
});
}
public render() {
return (
<div>
<div className="main-header">
<div className="inner">
<h1 className="main-title">Playpilot Assignment</h1>
<SearchForm onSubmit={this.handleSubmit} />
</div>
</div>
<div className="main-content">
[46]err {this.state.movie.map(movie => (
<div className="movieTitle">
<div className="movieCard">
<img
className="posterImg"
src={`https://image.tmdb.org/t/p/w185_and_h278_bestv2/${
movie.poster_path
}`}
alt={movie.title}
/>
<div className="searchFilmTitles" key={movie.id}>
{movie.title}
</div>
</div>
</div>
))}
{this.state.loading ? (
<h2>Loading...</h2>
) : (
<MoviesList data={this.state.moviesList} />
)}
</div>
</div>
);
}
}
SearchForm.tsx
import * as React from "react";
export default class SearchForm extends React.Component<any, any> {
public value: HTMLInputElement | null;
constructor(props: any) {
super(props);
this.state = {
movie: []
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
public handleChange(event: any) {
this.setState({ value: event.target.value });
}
public handleSubmit(event: any) {
event.preventDefault();
if (this.props.onSubmit && typeof this.props.onSubmit === "function") {
this.props.onSubmit(this.state.value);
}
}
public render() {
return (
<form className="search-form" onSubmit={this.handleSubmit}>
<label className="is-hidden" htmlFor="search">
Search
</label>
<input
type="search"
onChange={this.handleChange}
name="search"
ref={input => (this.value = input)}
placeholder="Search..."
/>
<button type="submit" id="submit" className="search-button">
<i className="material-icons icn-search" />
</button>
</form>
);
}
}
MoviesList.tsx
import * as React from "react";
import Movie from "./Movie";
const MoviesList = (props: any) => {
const results = props.data;
let movie: any;
[7]err if (results.data) {
// tslint:disable-next-line:no-shadowed-variable
movie = results.map((movie: any) => (
<Movie url={props.url} key={movie.id} />
));
} else {
movie = <h1>404: No movies found.</h1>;
}
return <ul className="movie-list">{movie}</ul>;
};
export default MoviesList;
再次感谢您的帮助!
1个回答
response.data
实际上不是数组,而是对象。将您的 API 调用更改为:
this.setState(
loading: false,
movie: response.data.results // results is the array you are looking for.
});
或者,也许更好的是,在渲染函数中映射结果:
{this.state.movie && this.state.movie.results.map(movie => (
...
))}
Hinrich
2018-06-06