I am creating a small project that uses the OMDB API to search for movies, and display them. However, whenever I want to show details about the title, I need to send the IMDB ID of the title I am trying to access to the API in the form of ttXXXXXXX
where 'X' refers to an integer ID.
However, the problem arises when I try to search titles which have leading zeroes in their IMDB IDs (for example tt0284753
which is "Lippy the Lion and Hardy Har Har"), since the JavaScript parseInt()
method removes leading zeroes, and the API needs the IMDB IDs in integer forms (maybe it performs RegEx in the background to remove leading "tt"s from the URL string (or whatever).
Currently, my implementation is like this:
//Show route
app.get("/results/:id",(req,res)=>{
let strID = req.params.id;
let imdbID = parseInt(strID.substr(2));
let url = `http://omdbapi.com/?i=tt${imdbID}&apikey=${apikey}`;
request(url,(error,response,body)=>{
if(!error && response.statusCode == 200){
let movie = JSON.parse(body);
res.render("details",{movie:movie,url:url});
}
})
});
You are using the ID inside a template-string, so in the end it is a string. Parsing it to and integer should not be necessary.