0

I have this code that uses thezipcodes.net to get latitude and longitude and then it puts it through api.weather.gov to get the weather. The problem is that it is screwing up in the area where it gets the points in the weather.gov API.

In getweatherforecaststuff() it returns a 404 error and is trying to set the ending to 0,0.

let lat = 0;
let long = 0;
let forecaststring = "";

function getLat(zipcode) {
  //add code here to get from api
  $.getJSON("https://thezipcodes.com/api/v1/search?zipCode=" + zipcode + "&countryCode=US&apiKey=01f2f327b789845bddf7d617257d8f76", function(data) {
    lat = data["location"][0]["latitude"];
    long = data["location"][0]["longitude"];
  });
}

function getWeatherForecastStuff() {
  $.getJSON("https://api.weather.gov/points/" + lat + "," + long + "", function(data) {
    forecaststring = data["properties"]["forecast"];
  });
}

function printStuff(forecaststrings) {
  $.getJSON(forecaststrings, function(data) {
    console.log(data);
  });
}

function getInfoNeeded() {
  zipcode = window.prompt("What is your zipcode?");
  getLat(parseInt(zipcode));
  getWeatherForecastStuff()
  printStuff(forecaststring)
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    `getJSON` is async so `lat` and `lng` aren't immediately available to the weather function. – Andy Feb 26 '22 at 20:56
  • WHat do you mean? – Mount Mario Feb 26 '22 at 20:57
  • You're calling `getWeather` immediately after `getLat` but `getLat` won't have completed its API call, and so `lat` and `lng` won't be defined when you call `getWeather` which is why it fails. The same goes for `printStuff` which won't have `forecast` available to it. – Andy Feb 26 '22 at 21:00

0 Answers0