themoviedb api 搜索电影

发布于 2025-01-11 20:36:46 字数 1057 浏览 4 评论 0原文

我被困住了。我正在使用 movie api 来搜索电影。我已经完成了api部分。但是,当我在搜索部分中随机输入一个无意义的单词时,我的应用程序会给出错误,因为没有这样的电影。我怎样才能克服这个困难?我在下面分享我的代码。

那是电影区;

 <div>
      <h4>{movie[0].title}</h4>
      <img
        src={`${base_URL}${movie[0].poster_path}`}
        alt={movie[0].title}
      />
    </div>

的搜索

<label>
      <span>Search Film:</span>
      <input
        type="text"
        value={search}
        onChange={(e) => setSearch(e.target.value)}
      />
    </label>

这就是来自Thats States

 const [movie, setMovie] = useState([{ title: "The Matrix Resurrections" }]); 

 const [search, setSearch] = useState("The Matrix Resurrections");

movie1

movie2

myError当我在搜索栏输入“sadsafasfas”时

I'm stuck. I am using movie api for search movie. I have done the api part. But when I type a random nonsense word in the search section, my application gives an error because there is no such movie. How can I get past this? I am sharing my codes below.

Thats movie area;

 <div>
      <h4>{movie[0].title}</h4>
      <img
        src={`${base_URL}${movie[0].poster_path}`}
        alt={movie[0].title}
      />
    </div>

Thats search from

<label>
      <span>Search Film:</span>
      <input
        type="text"
        value={search}
        onChange={(e) => setSearch(e.target.value)}
      />
    </label>

Thats States

 const [movie, setMovie] = useState([{ title: "The Matrix Resurrections" }]); 

 const [search, setSearch] = useState("The Matrix Resurrections");

movie1

movie2

myError when i type "sadsafasfas" search bar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2025-01-18 20:36:46

这是因为您使用 themoviedb api 搜索“无意义”值时得到的响应返回一个空对象。为了解决这个问题,您可以使用条件组件来检查 movie[0] 的值。你的代码可以这样写

<div>
{
   movie[0] ?
     <>
      <h4>{movie[0].title}</h4>
      <img
        src={`${base_URL}${movie[0].poster_path}`}
        alt={movie[0].title}
      /> 
     </>
       :
     <>
      <h4>Sorry, movie not found</h4> //your component if nothing returned from api
     </>
 }   
</div>

That is because the response you got from searching "nonsense" value using themoviedb api returning an empty object. To get through this, you can use a conditional component that checks the value of movie[0]. Your code can be written like this

<div>
{
   movie[0] ?
     <>
      <h4>{movie[0].title}</h4>
      <img
        src={`${base_URL}${movie[0].poster_path}`}
        alt={movie[0].title}
      /> 
     </>
       :
     <>
      <h4>Sorry, movie not found</h4> //your component if nothing returned from api
     </>
 }   
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文