Local Weather App: can retrieve latitude and longitude, but cannot use them for weather API call

So, I’m working on my local weather app. I’ve got the geolocation sorted out, but as you can see here,, I cannot get the weather API to respond, and my dev tools console is telling me that my lat and lng variables both return as ‘undefined’ when the API call tries to use them, even though they work just fine in the context of the getLoc function. I tried putting my API call function inside getLoc, which did nothing as well.

Any guidance would be most appreciated.

You are using two variables out of context for your API call, both lat and lon. I’m not saying you need to make them global, but have you tried putting them as argument for reqWeath ?

2 Likes

I went ahead and modified the code to make the latitude and longitude variables arguments passed to reqWeath, but the console still shows that they’re ‘undefined’ when reqWeath calls them.

I can’t seem to figure it out. It’s as though your variables are traped in your readystate function. even pushing them to an array in the global scope isn’t working. wow, this is strange.

I’m starting to think you may have inadvertently created some closures.

The problem is AJAX. You need to call the reqWeath() function inside the loation function’s if condition.
And change the if condition inside regWeath() from readystate to readyState (small mistake).

Okay, can you try calling reqWeath(lat,lng) on the last line of locOverIP() instead of calling it separately on line 47 ?

call the function on line 22 and it will work and no need to pass the variables as arguments

There are a couple of problems here. First, You’ve written this code as though it were going to run synchronously. You need to think asynchronously in JavaScript.

locOverIP();
reqWeath(lat, lng);

The second function isn’t going to wait for the XHR in the first function to finish before firing off. lat and lng won’t be defined until that request object has a response. The other problem I see is that you’ve defined the function reqWeath inside of the function locOverIp, and then you try to call it outside of its scope. I’m not sure whether this is intentional or you just missed a curly brace on line 28, but you actually do want to call reqWeath inside of locOverIp after the XHR object has its response.

1 Like

Heh, I was trying to implement the suggestions above and the scoping became a mess of spilled spaghetti. I decided to start fresh in a different pen keeping the old one open as a reference for the basic syntax features of the API calls and such. This is the new pen I’m trying to sort this out in.

So, if I understand correctly: by calling reqWeath inside of locOverIP as part of the if condition on the XHR, that should force it to wait to run until the first XHR has successfully completed, right? But now the code appears to be doing nothing. I’m not getting any errors in the console either, just nothing.

You wrote the reqWeath function, but didn’t invoke it. You’re on the right track, though!

1 Like

Ah, oops! I went ahead and invoked it, but I’m still not getting any actual behavior from my Javascript! Nothing shows up in the console at all now. I’m so confused . . .

you are calling the open and send function inside the if condition of location function.
call it after the if function.

1 Like

Thanks for all your help everyone! Take a look at it now, it’s not pretty but it’s functional.

1 Like

I have the same problem here. I got the latitude and longitude, but cannot use them in the API URL.