Weather api problem

Weather api problem
0.0 0

#1

Im just starting the weather app project and have run into a problem i can’t solve

https://jsfiddle.net/qhdyha0y/

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

https://fcc-weather-api.glitch.me/api/current?lat=undefined&lon=undefined

what is wrong with my weather variable? I can’t seem to figure it out.


#2

is that all the code?


#3

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.


#4

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.


#5

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.