首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果Axios.get响应数据良好,则反应setState

如果Axios.get响应数据良好,则反应setState
EN

Stack Overflow用户
提问于 2020-03-22 20:18:26
回答 2查看 45关注 0票数 1

目前,我正在发出axios请求,并将响应数据设置为我的状态。

我发现一些数据的'poster_path‘为null (无图像)。

我想在componentDidMount中的setState之前删除这些数据。

我已经尝试了以下方法,但是导致了错误。

代码语言:javascript
运行
复制
    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);
            })
        }
        )}

任何帮助都是非常感谢的。

代码语言:javascript
运行
复制
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数据如下所示

代码语言:javascript
运行
复制
{
  "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
      ],
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-22 20:26:24

你可以试试这个。只需在设置状态之前过滤您的电影数据。

代码语言:javascript
运行
复制
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);
            });
        }
    }
票数 2
EN

Stack Overflow用户

发布于 2020-03-22 20:27:37

您可以使用filter方法创建一个新数组,以删除poster_pathnull的电影,如果您想这样做的话。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60799204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档