getting info out of an API

Associate
Joined
19 Jul 2006
Posts
1,847
I have this codepen.
http://codepen.io/Casfan/pen/ZOWmdv?editors=1111

Its a project for freecodecamp where you have to get the local weather.
I know there are solutions out there showing exactly how to do it but I want to know why mine is not working so I can learn.

The get weather function works if you pass in a hardcoded city.

Its my get location that is not returning what I am expecting data.city should be the city name but its not working in this instance.
Any ideas on what I am doing wrong.

thanks
 
Associate
Joined
21 May 2013
Posts
1,991
"City" is blank for me (likely because looking up something like that from just an IP is not reliable at all). This is determined by the API provider (ipinfo.io) and is not something you can control.

There is nothing "wrong" with your code, you just need to decide what to do if the city cannot be determined (eg. have the user type it in, show an error message, etc.). It would be a good idea to properly handle other errors that might occur too (eg. lookup from weather APi fails, invalid city).
 
Last edited:
Back
Top Bottom