0

I have an api that requests weather data from openweathermap.org

var requestWeatherData = function(ipData){
      $.ajax({
        url: "http://api.openweathermap.org/data/2.5/weather",
        dataType: "json",        
        data: {
          q: ipData.city + ',' + ipData.countryCode,
          appid: "cant say"
        },
        success: function(wthrDetails) {                       
          addWeatherIcon(wthrDetails)             
        },
      });

An example reply from the the api is this:

{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}

But when I try to get the weather status via the reply from above:

var addWeatherIcon = function(weatherDetails) {
        var weatherType = weatherDetails.weather[0].main.toLowerCase();
        console.log(weatherType); //<-- outputs "clouds"
        console.log(typeof weatherType); //<---- outputs string
        console.log(weatherType == "clouds"); // <-- returns false
}

Why does console.log(weatherType == "clouds"); return false? The class is correct, and the output as well but it still returns false.Whats even weirder is sometimes when the internet is slow it returns true? Could it be Ajax needs to be slow inorder to detect it? . How is this possible?

EDIT: My code pen: http://codepen.io/nuclearmachine/full/ZKyrVp

chaosfirebit
  • 111
  • 2
  • 8

3 Answers3

0

Try:

var weatherType = weatherDetails.weather[0].main.toLowerCase().trim();

There must be a space somewhere.

informer
  • 821
  • 6
  • 18
0

weatherDetails.weather[0].main.toLowerCase()=="clouds" it will return true only, Please check your code

example fiddle is

$(document).ready(function(){
var details={  
   "coord":{  
      "lon":145.77,
      "lat":-16.92
   },
   "weather":[  
      {  
         "id":803,
         "main":"Clouds",
         "description":"broken clouds",
         "icon":"04n"
      }
   ],
   "base":"cmc stations",
   "main":{  
      "temp":293.25,
      "pressure":1019,
      "humidity":83,
      "temp_min":289.82,
      "temp_max":295.37
   },
   "wind":{  
      "speed":5.1,
      "deg":150
   },
   "clouds":{  
      "all":75
   },
   "rain":{  
      "3h":3
   },
   "dt":1435658272,
   "sys":{  
      "type":1,
      "id":8166,
      "message":0.0166,
      "country":"AU",
      "sunrise":1435610796,
      "sunset":1435650870
   },
   "id":2172797,
   "name":"Cairns",
   "cod":200
};
alert(details.weather[0].main.toLowerCase()=="clouds");
});

Check it https://jsfiddle.net/fdxuf7qn/

Biby Augustine
  • 425
  • 1
  • 3
  • 16
0

Oh my God, "thunderstorm" was spelled "thunderstom". Now the reason the application was sometimes displaying correctly is because other weather patterns were correctly spelled! I feel stupid, but lesson learned. Now the above question has clouds as its example, but I was testing thunderstorm in my computer GAAH.

chaosfirebit
  • 111
  • 2
  • 8