Else not working

I was trying to make a weather app but for some reason, my "else " is not working when the location is not available, here’s the code



                let lat = position.coords.latitude

                let long = position.coords.longitude


        .then(response => response.json())

        .then(weather => document.write(weather.main.temp))






Can you log out navigator.geolocation in this case? It’s been a long time since I’ve used it. Is it possible for the geolocation service to be present but not have the position?


Yes, it shows this when the if statement is fulfilled

Question for you. How do you know that navigator.geolocation does not exist in the device you are using? In other words, why do you think that you should be hitting the else statement?


I actually don’t fully understand what navigatr.geolocation is, I was just following the example form the internet that was showing how to get coordinates. I thought that navigator.geolocation is true when the user allows geolocation and false when he doesn’t

You are correct. Did you disable geolocation on the device you were using before testing the else statement?

Right, so it looks like the navigation api exists and has geolocation. As bbs says, maybe the actual location is disabled on your device.

But as far as why the else branch is not getting hit, it’s because navigator.geolocation is truthy.

What are you getting for lat and lon? Maybe you need to handle the case where geolocation exists but there is no lat and lon.

Maybe I misunderstood this question. Are you talking about checking whether geolocation is available in the first place (which would hit the else statement if it wasn’t) or are you talking about geolocation being available but not giving you valid coordinates?

Yeah, I’m a little confused too. I interpreted it as the latter because he was talking about the “location” being unavailable, not the service.

@veljkocukic, it would help if we knew exactly what error you are getting, what you’re seeing. What do you expect to happen, and what is happening instead.

It may be working just fine the whole time but I didn’t really know what the code I have written is supposed to do and I was expecting something else.
Because I am learning how to use geo location in JavaScript, expected the else to run when I click “Block” on thisScreenshot_34 .
Because of that, I was wondering why isn’t it wrong, as I said earlier, I thought that refusing to give location to the file would cause navigator.geolocation to be false and hence run everything coded under else. I apologize for taking your time, I am still learning :sweat_smile:

I think when you get to this point it means that your browser does support geolocation so you will never get to the else statement. If you decide to block the request then the call to getCurrentPosition will fail and you’ll need to handle that with a catch for the promise.

1 Like
    .catch(error => document.write(error))

I tried with this but it didn’t work either(did I even write this correctly?)

… navigator.geolocation to be false

If it wasn’t supported (like an old browser) I would expect it to be undefined.

There is code here that will handle a rejected permission. I tried it and got a nice message in the console: ERROR(1): User denied Geolocation

1 Like

Thanks guys! That did the job

Ahh, should have looked at the API a little closer. Forgot that you had to pass in a success and failure callback to getCurrentPosition. Good find @kevinSmith.

1 Like