简体   繁体   English

获取JavaScript未定义的TypeError

[英]Getting javascript undefined TypeError

Please help....Tried executing the below mentioned function but web console always shows 请帮助...。尝试执行以下提到的功能,但Web控制台始终显示

TypeError: xml.location.forecast[j] is undefined TypeError:xml.location.forecast [j]未定义

I was able to print the values in alert but the code is not giving output to the browser because of this error. 我能够打印警报中的值,但由于此错误,代码未将输出提供给浏览器。 Tried initializing j in different locations and used different increment methods.How can i get pass this TypeError 试图初始化j在不同的位置,并使用了不同的增量methods.How可i得到通过这个类型错误

Meteogram.prototype.parseYrData = function () {

var meteogram = this,xml = this.xml,pointStart;

if (!xml) {
    return this.error();
}

var j;

$.each(xml.location.forecast, function (i,forecast) {

j= Number(i)+1;

var oldto = xml.location.forecast[j]["@attributes"].iso8601;
var mettemp=parseInt(xml.location.forecast[i]["@attributes"].temperature, 10);

var from = xml.location.forecast[i]["@attributes"].iso8601;
var to = xml.location.forecast[j]["@attributes"].iso8601;

    from = from.replace(/-/g, '/').replace('T', ' ');
    from = Date.parse(from);
    to = to.replace(/-/g, '/').replace('T', ' ');
    to = Date.parse(to);

    if (to > pointStart + 4 * 24 * 36e5) {
        return;
    }
    if (i === 0) {
        meteogram.resolution = to - from;
    }


    meteogram.temperatures.push({
        x: from,
        y: mettemp,
        to: to,
        index: i
    });

if (i === 0) {
        pointStart = (from + to) / 2;
    }
});
this.smoothLine(this.temperatures);
this.createChart();
  };

You are trying to access the element after the last one. 您正在尝试访问最后一个元素之后的元素。 You can check if there is the element pointed by j before proceeding: 您可以在继续操作之前检查是否有j指向的元素:

Meteogram.prototype.parseYrData = function () {
    var meteogram = this,
        xml = this.xml,
        pointStart;

    if (!xml) {
        return this.error();
    }

    var i = 0;
    var j;

    $.each(xml.location.forecast, function (i, forecast) {
        j = Number(i) + 1;
        if (!xml.location.forecast[j]) return;

        var from = xml.location.forecast[i]["@attributes"].iso8601;
        var to = xml.location.forecast[j]["@attributes"].iso8601;
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM