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.
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 ?
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.
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).
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 reqWeathinside 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.
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.
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 . . .