themoviedb api 搜索电影
我被困住了。我正在使用 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");
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您使用 themoviedb api 搜索“无意义”值时得到的响应返回一个空对象。为了解决这个问题,您可以使用条件组件来检查
movie[0]
的值。你的代码可以这样写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