I’ve tried writing the js code for movie finder using fetch instead of XMLHttpRequest.
but inside the showMovies() function, the movie.imdbID returns ‘undefined’ value(screenshot attached).
const API_URL = "http://www.omdbapi.com/?&apikey=e6328603&s=";
const API_SEARCH_URL = "http://www.omdbapi.com/?apikey=e6328603&i=";
var searchInputBox = document.getElementsByClassName("input_search_text")[0];
var textBox = document.getElementById("text_view");
function searchMovies()
{
var query = searchInputBox.value;
alert(query);
if(query)
{
getMovies(API_URL+query);
alert(API_URL+query);
}
}
async function getMovies(url) {
const response = await fetch(url);
const respData = await response.json();
showMovies(respData.Search);
}
function showMovies(movies) {
alert("okkk");
movies.forEach(async function(movie) {
const movieData = await fetch(API_SEARCH_URL+movie.imdbID);
alert(API_SEARCH_URL+movie.imdbId);
const movieDataObj = await movieData.json();
displayMovies(movieDataObj);
});
}
function displayMovies(iMovie) {
alert("success in running fun displaymovies");
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<title>Movie Finder</title>
</head>
<body>
<div class="header_layout">
<div id="college_header_text">
<p><b>Movie Finder</b></p>
</div>
</div>
<div class="full_search_box">
<input type="text" name="search_text" class="input_search_text" placeholder="Search movies here..">
<button class="input_search_button" onclick="searchMovies()">
<b>Find!</b>
</button>
</div>
<div class="website_data_card">
<p><b id="text_view">The place to explore, filter, research, and browse all the Movies.</b></p>
</div>
</body>
<script type="text/javascript" src="js/script.js"></script>
</html>
Why isn’t it giving any vaid ‘movie.imdbID’ value?
EDIT1:‘movie.Title’ is fully accessible and returns some not null values.
EDIT2: according to this github thread there are some problematic imdbID’s. I just happen to encounter exactly one of them .
The issue has been solved (Ans. just don’t use the faulty movies).