FreeCodeCamp Weather API

Hey!

So I’m having an issue where I cannot source weather from other locations using their latitude and longitude. I have let geolocation get my location for me which worked fine, and then I used my googled latitude and longitude in a separate function and that worked fine as well. But when I use latitude and longitude from somewhere else, no data is returned. Anyone know what’s up?

Do you have a link to your project code?

yeah! I turned off the main page load function so I could test this with the japan button which has a handler near the bottom where you can input latitude and longitude.

When I click the Japan button, I get an error in the console due to the following line:

      weather.icon = data.weather[1].icon;

Always look at your browser console for errors. In this case, data.weather[1] is undefined. Add a console.log to see what data.weather looks like and you will see your issue.

1 Like

Okay, but here it is functioning with

weather.icon = data.weather[1].icon;

So, then from this point, how would one go about sourcing data from other coordinates?

I’m under the assumption that calling the getWeather function with new parameters will resubmit the request and the returned data will update my weather object.

data.weather is just an array of one or more objects. Some of those objects may have an icon property and some of those objects may not have an icon property. With the way you are targeting the index 1, you are assuming there will always have a 2nd objects and assuming that it will have an icon property.

You will ultimately have to decide how you want to assign various values depending on how many of the objects appear in the array (if any do) and then what to do if an object does not have an icon property.

My suggestion is to just use the first array element’s icon property. If the first element does not have an icon property but the second element does, then use its icon property value.

1 Like

All right, I think I understand what you’re saying. Thanks for the quick reply!