簡體   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