Errors with Weather API

Im trying to draw a circle based on data from a weather api and when i try to draw the circle i keep getting an error. I don’t know how to fix the mistake so here is the code. If you know how to fix it could you please let me know.

let weather;

function setup() {
  createCanvas(400, 400);
  loadJSON('http://api.openweathermap.org/data/2.5/weather?zip=85248,us&units=imperial&appid=27b5b65946faf6311b0225a9c450b652', gotData, 'pjson');
}

function gotData(data) {
  weather = data;
}

function draw() {
  background(51);

  tempeture = weather.main.temp;
  console.log(tempeture);
}
1 Like

First, temperature isn’t defined in the code you provided, make sure you include let temperature; at the top alongside let weather; (or let weather, temperature; to define both). That probably won’t throw an error though, so the error you’re most likely running into is that weather is still undefined on the first run through draw() because it hasn’t gotten the data yet (so trying to read weather.main.temp isn’t possible). You can solve that by making sure that weather has something in it before using it, like this:

if (weather) {
  tempeture = weather.main.temp;
  console.log(tempeture);
}
1 Like

That worked! thank you so much.