未捕获的类型错误:无法读取属性'条件'的未定义

Uncaught TypeError: Cannot read property 'condition' of undefined

本文关键字:属性 未定义 条件 读取 类型 错误      更新时间:2023-09-26

这是我的代码:

var location = 'RPXX0022'; // Find the code of your location in the following link and change this value between the single quotes: http://edg3.co.uk/snippets/weather-location-codes/
var unit = 'c';

var wq = "SELECT * FROM weather.forecast WHERE location='" + location + "' AND u='" + unit + "'";
var cb = Math.floor((new Date().getTime()) / 1200 / 1000);
var wu = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(wq) + '&format=json&_nocache=' + cb;
window['ywcb'] = function(data) {
    var info = data.query.results.channel.item.condition;
    var city = data.query.results.channel.location.city;
    var country = data.query.results.channel.location.country;
    $('.ywicon').addClass('wi-yw-' + info['code']);
    $('#ywloc').html(city + ", " + country);
    $('#ywtem').html(info.temp + '<span>' + '&deg;' + (unit.toUpperCase()) + '</span>');
};
$.ajax({
    url: wu,
    dataType: 'jsonp',
    cache: true,
    jsonpCallback: 'ywcb'
});

它给出了这个错误:

未捕获的类型错误:无法读取未定义的属性"condition"

检查项目对象中是否存在条件

 var info = data.query.results.channel.item.condition;

此外,在这一行中设置一个调试器,并检查您是否在信息变量中获得任何值