Does it log undefined or does it throw an Uncaught ReferenceError: shortURL is not defined ?
Do you see this log? console.log('2 ' + shortUrl)?
You’re using loose comparison here:
if (urlPairFound != null) {
var shortUrl = urlPairFound.shortUrl;
console.log('2 ' + shortUrl);
}
console.log('3 ' + shortURL);
If urlPairFound is undefined instead of null, this statement will be false and it’ll skip the if block. So I’d first check if urlPairFound is undefined.
shortUrl and shortURL are not the same identifier.
Edit: BTW, it’s not global it’s function scoped.
Can I ask why are you redeclaring the variable 4 times? Personally, I would not suggest doing that. If you want block scoped unique variables use let or const. Otherwise, declare the var variable one time and reassign its value.
A typo: I had written ‘shortURL’ instead of ‘shortUrl’ which was what I had used earlier when I declared the variable
I was trying to use the ‘shortUrl’ variable OUTSIDE of the function(err, urlPairFound) {...}
function, hence, the program had no clue what I meant by urlPairFound