Grizzly
Grizzly

Reputation: 5953

Unable to get property 'query' of undefined or null reference

I am using Yahoo Weather API.

Here is my code:

<script>
var callbackFunction = function (data) {
    console.log(data);

    var location = data.query.results.channel.location;
    var condition = data.query.results.channel.item.condition;
    var wind = data.query.results.channel.wind;
    var units = data.query.results.channel.units;

    document.getElementById('Weather-Info').innerHTML = 'The weather for ' + location.city + ', ' + location.region + ' is ' + condition.temp + units.temperature + ' with a wind speed of ' + wind.speed;
}
callbackFunction()

<script src="https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20woeid%3D%222409681%22%20and%20u%3D%22f%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=callbackFunction"></script>

Everytime I go into the page I receive the following pop-up:

Unable to get property 'query' of undefined or null reference

But if I hit No, the data is still populated.. how is it populating data if it is telling me that a property is undefined? The YQL Weather API is a really complicated.

Upvotes: 0

Views: 837

Answers (1)

kagelos
kagelos

Reputation: 423

Why are you calling the callback function yourself? In any case, calling it without a parameter, means that data will be undefined and this is why you get the error.

I know nothing about Yahoo! weather API but this seems like a JSONP thing, so try removing this part:

callbackFunction()

since this will be called automatically once the script loads.

Upvotes: 2

Related Questions