-1

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});
        }
    })

});
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
dnak
  • 23
  • 7
  • Does this answer your question? [How to output numbers with leading zeros in JavaScript](https://stackoverflow.com/questions/2998784/how-to-output-numbers-with-leading-zeros-in-javascript) – jonrsharpe May 22 '20 at 20:35
  • No, it does not to be honest. However, it was a small and easy fix anyways. And I'm stupid. – dnak May 22 '20 at 20:50

2 Answers2

0

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.

max
  • 310
  • 1
  • 6
  • This is weird. It actually works! So basically, this is the same as `let imdbID = req.params.id;` and making the URL as `http://omdbapi.com/?i=${imdbID}&apikey=${apikey}`; But it didn't actually work the last time I tried? Hence I had to go with this weird workaround. Okay, now I am mad. – dnak May 22 '20 at 20:45
0

Numbers don't have "leading zeros". They only have a value. Textual representations of numbers may have leading zeroes.

Simple keep it as a string and either (or both):

  • Allow Javascript to coerce it to a number when used in a numeric context or...

  • Explicitly coerce it to a number when it needs to be a number.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135