【问题标题】:30 movies per page on OMDb api in ReactReact 中 OMDb api 上每页 30 部电影
【发布时间】:2021-07-11 22:53:09
【问题描述】:

我正在尝试合并页面以将项目增加到 20、30,因为 OMDb 每页只有 10 个。 我需要从第 1,2 和 3 页获取所有“imdbID”。

API 显示此结果:

```
"Search": [
{
"Title": "Home",
"Year": "2009",
"imdbID": "tt1014762",
"Type": "movie",
"Poster": "https://m.media-amazon.com/images/M/MV5BODY2ZTNmMjEtMWEyNC00YjZlLWEyMGEtZWFiNGMwYmQ5NWViXkEyXkFqcGdeQXVyMTAwMzM3NDI3._V1_SX300.jpg"
},
]


//my code

async function getRequest(){
  const pageOne = await axios
    .get('http://www.omdbapi.com/?s=${searchText}&apikey=ef773fae&page=1')
    .then(res=>res.data)
    .catch(error=>console.log(error));

  const pageTwo = await axios
      .get('http://www.omdbapi.com/?s=${searchText}&apikey=ef773fae&page=2')
      .then(res=>res.data)
      .catch(error=>console.log(error));

  const pageThree = await axios
      .get('http://www.omdbapi.com/?s=${searchText}&apikey=ef773fae&page=3')
      .then(res=>res.data)
      .catch(error=>console.log(error));

  const sumPage= {pageOne, pageTwo, pageThree};


//*not sure how to write this line of code to get all the imdbID*
  return sumPage.map(movie => movie.imdbID);
}

getRequest();

【问题讨论】:

    标签: javascript reactjs omdbapi


    【解决方案1】:

    将它们散布到一个数组中怎么样

    return {"Search":[...pageOne.Search, ...pageTwo.Search, ...pageThree.Search]};
    

    【讨论】:

    • 谢谢!我将const moviesList = nameSearch.Search.map(movie=>movie.imdbID); 添加到您的代码中,我将setState 添加到moviesList 并且它有效!现在我可以在一页中看到 30 部电影。
    猜你喜欢
    • 2020-02-27
    • 2021-03-02
    • 2017-08-13
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多