I have tried to create to create a component in vanilla JavaScript here in main js file I am trying to call a function that is defined in a component and also I am expecting an output but it gives me error saying showMovies
is not a function. Can anyone tell me what is right way to do so:
JavaScript
x
17
17
1
import Component from './Component.js';
2
3
form.addEventListener("submit", (e) => {
4
e.preventDefault();
5
main.innerHTML = "";
6
7
const searchTerm = search.value;
8
const SEARCHAPI = "https://api.themoviedb.org/3/search/movie?&api_key=04c35731a5ee918f014970082a0088b1&query=";
9
10
if (searchTerm) {
11
Component.showMovies(SEARCHAPI + searchTerm);
12
search.value = "";
13
} else {
14
main.innerHTML = "<h1>No result Found!</h1>";
15
}
16
});
17
JavaScript
1
43
43
1
const Component = () => {
2
3
const IMGPATH = "https://image.tmdb.org/t/p/w1280";
4
// const SEARCHAPI = "https://api.themoviedb.org/3/search/movie?&api_key=04c35731a5ee918f014970082a0088b1&query=";
5
6
const main = document.getElementById("main");
7
const form = document.getElementById("form");
8
const search = document.getElementById("search");
9
10
const showMovies = (url) => {
11
fetch(url)
12
.then((res) => res.json())
13
.then(function(data) {
14
console.log(data.results);
15
const el = document.createElement("div");
16
const title = document.createElement("h3");
17
18
const rank = document.createElement("h3");
19
const image = document.createElement("img");
20
const year = document.createElement("h3");
21
22
image.src = IMGPATH + data.results[0].poster_path;
23
el.appendChild(image);
24
25
let date = new Date(data.results[0].release_date);
26
27
rank.innerHTML = "Rating:  " + data.results[0].popularity;
28
29
title.innerHTML = "Title:  " + data.results[0].title;
30
year.innerHTML = "Release:  " + date.getFullYear();
31
32
main.appendChild(el);
33
main.appendChild(title);
34
main.appendChild(rank);
35
main.appendChild(year);
36
});
37
};
38
39
40
}
41
42
export default Component;
43
Advertisement
Answer
The component you have defined here is a function that consists a function called showMovies
. But as you have not returned the function, it will be inaccessible. To be able to use it, you may return the function.
JavaScript
1
43
43
1
const Component = ()=>{
2
3
const IMGPATH = "https://image.tmdb.org/t/p/w1280";
4
// const SEARCHAPI = "https://api.themoviedb.org/3/search/movie?&api_key=04c35731a5ee918f014970082a0088b1&query=";
5
6
const main = document.getElementById("main");
7
const form = document.getElementById("form");
8
const search = document.getElementById("search");
9
10
const showMovies = (url) => {
11
fetch(url)
12
.then((res) => res.json())
13
.then(function (data) {
14
console.log(data.results);
15
const el = document.createElement("div");
16
const title = document.createElement("h3");
17
18
const rank = document.createElement("h3");
19
const image = document.createElement("img");
20
const year = document.createElement("h3");
21
22
image.src = IMGPATH + data.results[0].poster_path;
23
el.appendChild(image);
24
25
let date = new Date(data.results[0].release_date);
26
27
rank.innerHTML = "Rating:  " + data.results[0].popularity;
28
29
title.innerHTML = "Title:  " + data.results[0].title;
30
year.innerHTML = "Release:  " + date.getFullYear();
31
32
main.appendChild(el);
33
main.appendChild(title);
34
main.appendChild(rank);
35
main.appendChild(year);
36
});
37
};
38
39
return { showMovies };
40
}
41
42
export default Component;
43
Now you can use the function using Component().showMovies(SEARCHAPI + searchTerm)