Cannot log open weather API in my page


#1

I am working on the weather forecast project.
I am able to get the coordinate of my location but then I cannot use them to get the weather there.
If I try console.log, what I get is “jQuery.Deferred exception: loc is undefined” “@pen.js:27:6
j@https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js:2:29997
g/</k<@https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js:2:30313
” undefined

Here is the link to my pen, an help would be really much appreciated.


#2

Hi, the weather challenge was updated recently. FCC created it’s own API that will hopefully solve your issue. You can access that new API at https://fcc-weather-api.glitch.me

Here is except from the updated challenge:

Note: Many internet browsers now require an HTTP Secure (https://) connection to obtain a user’s locale via HTML5 Geolocation. For this reason, we recommend using HTML5 Geolocation to get user location and then use the freeCodeCamp Weather API https://fcc-weather-api.glitch.me which uses an HTTP Secure connection for the weather. Also, be sure to connect to CodePen.io via https://.


#3

Thanks @marigerr but the problem is not the api itself, since if I write directly an example (wothout variables) it does work.
I suggest that the problem is the way I wrote the function but cannot figure out myself


#4

ah ok, I see now a problem. You need to wait to make your call to the weather API until after you have received the coordinates from ipinfo.io One way to do this is to move it into the ipinfo.io callback directly under line 12. If you still have problems, you might try using the new fcc weather api.


#5

Ok you mean this?

var key = “c68c9b83e1b2903741314ae01c58f011”;

$(document).ready(function() {

var loc;

$.getJSON(“https://ipinfo.io”, function(data) {

console.log(“get some interesting data…”);
loc = data.loc.split(" , ");
console.log(loc)

 $.getJSON("https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat="+loc[0]+"&lon="+loc[1]+"&appid=c68c9b83e1b2903741314ae01c58f011", function(weatherData)
       //api.openweathermap.org/data/2.5/weather?lat=35&lon=139
           
        {

 console.log(weatherData)

})

});

});

Really appreciate your help, thank a lot