简体   繁体   English

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

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

I have downloaded the following web-based-face-detect-program and compiled it and my compiler shows no syntax errors, however when I try upload a picture with the application I get a Cannot read property 'length' of undefined error . 我已经下载了以下基于网络的人脸检测程序并进行了编译,并且编译器未显示语法错误,但是当我尝试使用应用程序上载图片时,出现Cannot read property 'length' of undefined error I haven't changed to application at all so there should not be an issue and I am fairly new to Javascript so I could be making a rookie mistake. 我根本没有更改过应用程序,因此应该没有问题,而且我对Javascript还是很陌生,所以我可能会犯一个菜鸟错误。 Heres the js function code: 这是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);
}

The line .find(".detected-faces").html("").append("<p>Found " + (data.images.length-1) + " faces:</p>"); 这行.find(".detected-faces").html("").append("<p>Found " + (data.images.length-1) + " faces:</p>"); is what causes the error. 是什么导致错误。 And heres the errors I get in the Chrome console: 这里是我在Chrome控制台中看到的错误:

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

The rest of the code is all on the github link if theres something I have missed out. 如果我错过了某些内容,其余代码全部在github链接上。 So can someone please explain to me why I am getting this undefined error and how I can resolve it? 因此,有人可以向我解释为什么我遇到此未定义的错误以及如何解决该错误吗?

It seems that the data object have not the images property. 似乎数据对象没有images属性。 Before the error line, use the JSON.stringify(data) function to check the data object structure. 在错误行之前,使用JSON.stringify(data)函数检查数据对象结构。

You can use: 您可以使用:

console.log(JSON.stringify(data))

Or: 要么:

alert(JSON.stringify(data))

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

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