目前,我正在发出axios请求,并将响应数据设置为我的状态。
我发现一些数据的'poster_path‘为null (无图像)。
我想在componentDidMount中的setState之前删除这些数据。
我已经尝试了以下方法,但是导致了错误。
componentDidMount() {
let url = "".concat(baseURL, '?api_key=', APIKEY, configData, `&with_genres=${genreID}`)
axios.get(url)
.then(res =>
if (res.data.results.poster_path != null){
this.setState({movies: res.data.results},()=>{
console.log(this.state.movies);
})
}
)}
任何帮助都是非常感谢的。
export class Documentary extends Component {
constructor (props) {
super (props)
this.state = {
movies:[]
};
}
componentDidMount() {
let url = "".concat(baseURL, '?api_key=', APIKEY, configData, `&with_genres=${genreID}`)
axios.get(url)
.then(res =>
this.setState({movies: res.data.results},()=>{
console.log(this.state.movies);
})
)}
.....render...
API数据如下所示
{
"page": 1,
"total_results": 10000,
"total_pages": 500,
"results": [
{
"popularity": 27.169,
"vote_count": 0,
"video": false,
"poster_path": "/4RtCBnCnsEqcTQejPQ1lIiCE75r.jpg",
"id": 680438,
"adult": false,
"backdrop_path": "/aejCGlzSRY3nVmB23IpBiXuoXzW.jpg",
"original_language": "en",
"original_title": "After Truth: Disinformation and the Cost of Fake News",
"genre_ids": [
99
],
"title": "After Truth: Disinformation and the Cost of Fake News",
"vote_average": 0,
"overview": "An investigation into the ongoing threat caused by the phenomenon of “fake news” in the U.S., focusing on the real-life consequences that disinformation, conspiracy theories and false news stories have on the average citizen.",
"release_date": "2020-03-19"
},
{
"popularity": 25.04,
"vote_count": 0,
"video": false,
"poster_path": "/rODi66xlhEIaHs2krHlj4A8da5f.jpg",
"id": 674334,
"adult": false,
"backdrop_path": "/rjtXLRO0ixmbjoQM27Rj7rFRWe9.jpg",
"original_language": "en",
"original_title": "Climbing Blind",
"genre_ids": [
99
],
"title": "Climbing Blind",
"vote_average": 0,
"overview": "Blind climber Jesse Dufton's ascent of the Old Man of Hoy.",
"release_date": "2020-03-20"
},
{
"popularity": 24.492,
"vote_count": 0,
"video": false,
"poster_path": null,
"id": 674338,
"adult": false,
"backdrop_path": null,
"original_language": "en",
"original_title": "Amber and Me",
"genre_ids": [
99
],
发布于 2020-03-22 20:26:24
你可以试试这个。只需在设置状态之前过滤您的电影数据。
axios.get(url)
.then(res => {
if (res.data) {
const movieData = res.data.results.filter(movie => movie.poster_path != null);
this.setState({movies: movieData}, () => {
console.log(this.state.movies);
});
}
}
发布于 2020-03-22 20:27:37
您可以使用filter
方法创建一个新数组,以删除poster_path
为null
的电影,如果您想这样做的话。
https://stackoverflow.com/questions/60799204
复制相似问题