繁体   English   中英

无法读取未定义错误的属性“长度”

[英]Cannot read property 'length' of undefined error

我已经下载了以下基于网络的人脸检测程序并进行了编译,并且编译器未显示语法错误,但是当我尝试使用应用程序上载图片时,出现Cannot read property 'length' of undefined error 我根本没有更改过应用程序,因此应该没有问题,而且我对Javascript还是很陌生,所以我可能会犯一个菜鸟错误。 这是js函数代码:

function parse_response(res) {
    var data = jQuery.parseJSON(res);

    if (data.success == false) {
        show_page(form);
        show_message(data.msg);
        return;
    }

    results
        .find(".source-img").html("").end()
        .find(".detected-faces").html("").append("<p>Found " + (data.images.length-1) + " faces:</p>");

    $.each(data.images, function(i, img) {
        var el = '<img src="face-detect/' + img.src + '?r=' + Math.random() + '" alt="" />';
        results.find((i == 0 ? ".source-img" : ".detected-faces")).append(el);
    })

    results.find(".source-img img").bind('click', function(e) {
        var im = data.images[0];
        var el = '<img src="face-detect/' + im.src + '" width="' + im.width + '" height="' + im.height + '" alt="" />';
        $(".lightbox", results).find(".lightbox-content").html(el).end().lightbox("show");
    });

    show_page(results, actions);
}

这行.find(".detected-faces").html("").append("<p>Found " + (data.images.length-1) + " faces:</p>"); 是什么导致错误。 这里是我在Chrome控制台中看到的错误:

Uncaught TypeError: Cannot read property 'length' of undefined app.js:87
parse_response app.js:87
xhr.onreadystatechange app.js:65

如果我错过了某些内容,其余代码全部在github链接上。 因此,有人可以向我解释为什么我遇到此未定义的错误以及如何解决该错误吗?

似乎数据对象没有images属性。 在错误行之前,使用JSON.stringify(data)函数检查数据对象结构。

您可以使用:

console.log(JSON.stringify(data))

要么:

alert(JSON.stringify(data))

暂无
暂无

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

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