Im just starting the weather app project and have run into a problem i can’t solve
the getLocation function runs in the console. I can get my LAT and LONG variables to change to my location, but when I put these variables in my var weather URL, I get undefined.
when i console weather after running getLocation i get this URL
what is wrong with my weather variable? I can’t seem to figure it out.
If you put
console.log statements where your lat and long variables are set, you’ll see that that is running after your AJAX call. A hard concept to understand is asynchronous functions, but it is very important in JS. A simple way to handle this is to put anything that needs those variables (like the AJAX) call inside the callback function to navigator. This pen will help you get the idea.
Thank you Kevin. I’ll be honest. I never heard of AJAX before. Thank you for your response, I’m gonna have to read up on this and digest it.
It’s a lot to digest and is a little different from how a lot of other languages handle these types of things. It’s weird, but once you get used to it, it is one of the most powerful features of JS. But everyone struggles with it at first.