Not sure what the problem is The code work fine until I try and use the filteredMovies in the displayMovies function not sure what is going on here. I am new to coding and am trying to get the api to work. Everything looks good until I get to this point. Not sure if there is another way to go about doing this or what I have to do to fix this but this has been very furstrating.
JavaScript
x
42
42
1
const url =
2
'https://yts.mx/api/v2/list_movies.json?sort_by=download_count&limit=25';
3
let ytxMovies = [];
4
const movies = 'movies';
5
searchBar.addEventListener('keyup', (e) => {
6
const searchString = e.target.value;
7
const filteredMovies = ytxMovies.data.movies.filter((list_movies) => {
8
return list_movies.title_english.includes(searchString);
9
});
10
console.log(filteredMovies);
11
displayMovies(filteredMovies) says movies on line 27 is undifined
12
});
13
14
const loadMovies = async () => {
15
try {
16
const res = await fetch(url);
17
ytxMovies = await res.json();
18
displayMovies(ytxMovies);
19
// console.log(ytxMovies);
20
} catch (err) {
21
console.log(err);
22
}
23
};
24
25
const displayMovies = (movie_list) => {
26
// this is the only way to display all the movies
27
const htlmString = movie_list.data.movies
28
.map((movie_list) => {
29
return `
30
<li class="movie_list">
31
<h2>${movie_list.title}
32
<p>${movie_list.description_full}
33
<img src="${movie_list.medium_cover_image}"></img>
34
</li>
35
`;
36
})
37
.join('');
38
movieList.innerHTML = htlmString;
39
};
40
41
loadMovies();
42
Advertisement
Answer
You’re filtering the ytxMovies.data.movies
in the filteredMovies
so the displayMovies
will get ytxMovies.data.movies
when you pass it so, it doesn’t have the data.movies
when you map
it!
Solution:
Change loadMovies
to this:
JavaScript
1
10
10
1
const loadMovies = async () => {
2
try {
3
const res = await fetch(url)
4
ytxMovies = await res.json()
5
displayMovies(ytxMovies.data.movies)
6
} catch (err) {
7
console.log(err)
8
}
9
}
10
This way it will pass an array to the displayMovies
, then change the displayMovies
to:
JavaScript
1
16
16
1
const displayMovies = (movie_list) => {
2
// this is the only way to display all the movies
3
const htlmString = movie_list
4
.map((movie) => {
5
return `
6
<li class="movie">
7
<h2>${movie.title}
8
<p>${movie.description_full}
9
<img src="${movie.medium_cover_image}"></img>
10
</li>
11
`
12
})
13
.join('')
14
movieList.innerHTML = htlmString
15
}
16
I changed your map
a bit in displayMovies
, the whole code:
JavaScript
1
63
63
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8" />
5
<meta
6
name="viewport"
7
content="width=device-width, initial-scale=1.0"
8
/>
9
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
10
<title>Document</title>
11
</head>
12
<body>
13
<input id="search-bar" type="search" />
14
<ul id="movie-list">
15
Loading
16
</ul>
17
<script>
18
let searchBar = document.getElementById('search-bar')
19
let movieList = document.getElementById('movie-list')
20
const url =
21
'https://yts.mx/api/v2/list_movies.json?sort_by=download_count&limit=25'
22
let ytxMovies = []
23
const movies = 'movies'
24
searchBar.addEventListener('keyup', (e) => {
25
const searchString = e.target.value
26
const filteredMovies = ytxMovies.data.movies.filter((movie) =>
27
movie.title_english.includes(searchString)
28
)
29
displayMovies(filteredMovies)
30
// says movies on line 27 is undifined
31
})
32
33
const loadMovies = async () => {
34
try {
35
const res = await fetch(url)
36
ytxMovies = await res.json()
37
displayMovies(ytxMovies.data.movies)
38
} catch (err) {
39
console.log(err)
40
}
41
}
42
43
const displayMovies = (movie_list) => {
44
// this is the only way to display all the movies
45
const htlmString = movie_list
46
.map((movie) => {
47
return `
48
<li class="movie">
49
<h2>${movie.title}
50
<p>${movie.description_full}
51
<img src="${movie.medium_cover_image}"></img>
52
</li>
53
`
54
})
55
.join('')
56
movieList.innerHTML = htlmString
57
}
58
59
loadMovies()
60
</script>
61
</body>
62
</html>
63