Javascript – 需要点击两次提交按钮才能打印HTML – OMDB API

huangapple go评论63阅读模式
英文:

Javascript - Need to click submit form twice to get html printed - OMDB api

问题

以下是您的代码的中文翻译部分:

// 我正在尝试创建一个用于练习的网站,该网站从OMDB API获取电影,并在用户搜索后在页面上显示HTML。

// 问题是用户需要点击搜索按钮两次才能显示HTML。

// API响应并填充我的数组(fullMovies),但HTML(movieItemHTML函数)不会在第一次点击时显示,而是在第二次点击时显示。

// 我猜测这与async await有关,但在过去的3小时中,我无法解决它。
// 欢迎任何帮助!

const form = document.getElementById('form');
const movieItem = document.querySelector('.movies');
const APIKEY = 'a3f1f778';
const exploring = document.querySelector('.exploring');
const searchInput = document.getElementById('query');

const movies = [];
const fullMovies = [];

form.addEventListener('submit', (e) => {
  e.preventDefault();

  getMovies();
});

const getMovies = async () => {
  const url = `http://www.omdbapi.com/?s=${searchInput.value}&apikey=${APIKEY}`;
  const res = await fetch(url);
  const movieData = await res.json();
  movieData.Search.forEach((e) => movies.push(e));
  searchMovieDetails();
};

const searchMovieDetails = () => {
  movies.forEach(async (e) => {
    const res = await fetch(
      `http://www.omdbapi.com/?t=${e.Title}&apikey=${APIKEY}`
    );
    const data = await res.json();
    fullMovies.push(data);
  });
  movieItemHTML();
};

function movieItemHTML() {
  exploring.style.display = 'none';
  let html = '';
  fullMovies.forEach((e) => {
    html = `
    <div class="movie-item">
      <img class="poster" src=${e.Poster}/>
      <div class="movie-desc">
        <div class="title-rating">
          <h3>${e.Title}</h3>
          <img src="./images/star.png" class="star"/>
          <p class="rating"> ${e.imdbRating}</p>
        </div>
        <div class="time-kind-btn">
          <p>${e.Runtime}</p>
          <p>${e.Genre}</p>
          <a class="watchlist"><img src="./images/addBtn.png"> Watchlist</a>
        </div>
        <div>
          <p class="plot">${e.Plot}</p>
        </div>
      </div>
    </div>
    `;
    movieItem.innerHTML += html;
  });
}

请注意,我已经将HTML标记和JavaScript代码翻译为中文。如果您需要进一步的帮助,请随时提出。

英文:

I am trying to make a site for practice that gets the movies from the OMDB api and displays the HTML on the page once the user searches for a term.

The problem is that the user needs to click the search button twice for the html to appear.

The api responds and populates my Array (fullMovies) but the html (movieItemHTML function) does not get displayed on the first click but on the second

I am guessing it has something to do with async await but for the past 3 hours I can not figure it out.
Any help appreciate it!

const form = document.getElementById(&#39;form&#39;);
const movieItem = document.querySelector(&#39;.movies&#39;);
const APIKEY = &#39;a3f1f778&#39;;
const exploring = document.querySelector(&#39;.exploring&#39;);
const searchInput = document.getElementById(&#39;query&#39;);
const movies = [];
const fullMovies = [];
form.addEventListener(&#39;submit&#39;, (e) =&gt; {
e.preventDefault();
getMovies();
//   console.log(fullMovies);
});
const getMovies = async () =&gt; {
const url = `http://www.omdbapi.com/?s=${searchInput.value}&amp;apikey=${APIKEY}`;
const res = await fetch(url);
const movieData = await res.json();
movieData.Search.forEach((e) =&gt; movies.push(e));
searchMovieDetails();
};
const searchMovieDetails = () =&gt; {
movies.forEach(async (e) =&gt; {
const res = await fetch(
`http://www.omdbapi.com/?t=${e.Title}&amp;apikey=${APIKEY}`
);
const data = await res.json();
fullMovies.push(data);
});
movieItemHTML();
};
function movieItemHTML() {
exploring.style.display = &#39;none&#39;;
let html = &#39;&#39;;
fullMovies.forEach((e) =&gt; {
html = `
&lt;div class=&quot;movie-item&quot;&gt;
&lt;img class=&quot;poster&quot; src=${e.Poster}/&gt;
&lt;div class=&quot;movie-desc&quot;&gt;
&lt;div class=&quot;title-rating&quot;&gt;
&lt;h3&gt;${e.Title}&lt;/h3&gt;
&lt;img src=&quot;./images/star.png&quot; class=&quot;star&quot;/&gt;
&lt;p class=&quot;rating&quot;&gt; ${e.imdbRating}&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;time-kind-btn&quot;&gt;
&lt;p&gt;${e.Runtime}&lt;/p&gt;
&lt;p&gt;${e.Genre}&lt;/p&gt;
&lt;a class=&quot;watchlist&quot;&gt;&lt;img src=&quot;./images/addBtn.png&quot;&gt; Watchlist&lt;/a&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;p class=&quot;plot&quot;&gt;${e.Plot}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
`;
movieItem.innerHTML += html;
});
}

答案1

得分: 1

这是代码部分,不需要翻译:

function getMovies(searchInput, APIKEY){
  fetch(`http://www.omdbapi.com/?s=${searchInput.value}&amp;apikey=${APIKEY}`)
  .then(r =&gt; r.json())
  .then(j =&gt; Promise.all(j.Search.map(m =&gt; fetch(`http://www.omdbapi.com/?t=${m.Title}&amp;apikey=${APIKEY}`)
                                           .then(r =&gt; r.json())
                                     )
                        )
   )
  .then(movies =&gt; movies.forEach(appendMovieToWhateverDOMElement))
  .catch(handleError);
}

翻译好的部分:

"这是因为searchMovieDetails()函数在fullMovies数组填充之前同步调用了movieItemHTML()函数。不好意思,但这是一段代码在很多方面都很糟糕。主要是副作用会让你误以为你在第二次点击时会得到它们。

除非你确实需要它们,否则要远离异步函数。异步操作不应该被实现得像同步命令式代码一样。这是 JavaScript 的一个缺陷。

你可以使用一个函数来完成这个任务。"

英文:

It happens because the searchMovieDetails() function invokes movieItemHTML() function synchronously even before the fullMovies array gets populated. And sorry but this is a terrible code on many levels. Primiarily the side effects deceive you to believe that you get them on the second click.

Stay away from the async functions unless you really need them if you ever do. Asynchronous operations shouldn't be implemented as if they are synchronous imperative code. This is a wart of JS.

You can use a single function for this job.

function getMovies(searchInput, APIKEY){
fetch(`http://www.omdbapi.com/?s=${searchInput.value}&amp;apikey=${APIKEY}`)
.then(r =&gt; r.json())
.then(j =&gt; Promise.all(j.Search.map(m =&gt; fetch(`http://www.omdbapi.com/?t=${m.Title}&amp;apikey=${APIKEY}`)
.then(r =&gt; r.json())
)
)
)
.then(movies =&gt; movies.forEach(appendMovieToWhateverDOMElement))
.catch(handleError);
}

huangapple
  • 本文由 发表于 2023年3月4日 02:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定