无法读取未定义的属性/未定义的属性


cannot read property of undefined/property not defined

我的数组正在返回信息并且其中包含数据(不是空(,但它仍然错误地说:

捕获的类型错误:无法读取未定义的属性"date_appeal_received"或"未捕获的引用错误:未定义date_appeal_received。

我试过:

console.log(result.parcel[1].date_appeal_received);  
console.log(result.parcel[1][date_appeal_received]);  
console.log(result.parcel[1]["date_appeal_received"]);  

如果我只是输入数组,它将完全返回数组console.log(result);但我需要将选择属性值放入输入框中。

代码如下:

.PHP

<?php
require_once('../config.php');
if(isset($_GET['parcel_id'])) {
    $db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    $parcel = $db->get_results("select * from parcels where parcel_id=" . $_GET['parcel_id']);
    $comment_count = $db->get_var("select count(*) as count from comments where parcel_id=" . $_GET['parcel_id']);
    echo json_encode(array('parcel'=>$parcel,'comment_count'=>$comment_count));
}
?>

j查询:

$.ajax({
    url: "classes/get-apn-count-comments.php?parcel_id=" + parcel_id,
    type: "GET",
    data: { parcel_id : parcel_id },
    dataType: 'json',
    error: function(SMLHttpRequest, textStatus, errorThrown){
        alert("An error has occurred making the request: " + errorThrown);
    },
    success: function(result){
            //do stuff here on success
            //console.log(result);
            $('#ViewComments').val('View ' + result.comment_count + ' Comments');
            if (result.parcel[0].apn != null){ $('#ParcelNumber').html(result.parcel[0].apn)
            } else { $('#ParcelNumber').html(" "); }
            console.log(result.parcel[1].date_appeal_received);
            /*if (result.parcel[1].date_appeal_received != null) { $('#DateAppealReceived').html(result.parcel[1].date_appeal_received);
            } else { $('#DateAppealReceived').html(" "); }
            if (result.parcel[2].date_appeal_received != null) { $('#BosMeetingDate').html(result.parcel[2].bos_meeting_date);
            } else { $('#BosMeetingDate').html(" "); }
            if (result.parcel[3].late_returns_date != null) { $('#LateReturnsDate').html(result.parcel[3].late_returns_date);
            } else { $('#LateReturnsDate').html(" "); }
            if (result.parcel[4].determination_notice_sent_date != null) { $('#DeterminationNoticeSent').html(result.parcel[4].determination_notice_sent_date);
            } else { $('#DeterminationNoticeSent').html(" "); }
            if (result.parcel[5].final_determination != null) { $('#FinalDetermination').html(result.parcel[5].final_determination);
            } else { $('#FinalDetermination').html(" "); }
            if (result.parcel[6].analysis_recommendation != null) { $('#AnalysisRecommendation').html(result.parcel[6].analysis_recommendation);
            } else { $('#AnalysisRecommendation').html(" "); }
            if (result.parcel[7].email_address != null) { $('#EmailAddress').html(result.parcel[7].email_address);
            } else { $('#EmailAddress').html(" "); }
            if (result.parcel[8].phone_number != null) { $('#PhoneNumber').html(result.parcel[8].phone_number);
            } else { $('#PhoneNumber').html(" "); }*/
    }
});

数组结果:

{
    "parcel": [{
        "parcel_id": "89415",
        "tax_year": "2012",
        "apn": "134-44-114",
        "vin": null,
        "owner_name": "BARRERA DIANE",
        "in_care_of": null,
        "mailing_address_1": "2402 W RIVIERA DR",
        "mailing_address_2": null,
        "city_state_zip": "TEMPE AZ 85282",
        "suite": null,
        "country": null,
        "initial_mail_date": "2012-05-11",
        "initial_return_date": null,
        "final_mail_date": "2012-07-13",
        "final_return_date": null,
        "reclass_mail_date": "2012-09-11",
        "bos_meeting_date": "2012-10-17",
        "date_appeal_received": "2012-09-18",
        "late_returns_date": null,
        "determination_notice_sent_date": null,
        "response_code": "No Response",
        "email_address": "dianefm@cox.net",
        "phone_number": "4807734925",
        "notification_type": null,
        "analysis_recommendation": "3",
        "final_determination": "A",
        "account": null,
        "revised_legal_class": null,
        "other": null,
        "change_address_request": null,
        "determination_notice_sent": null,
        "appeal_address_match": null,
        "situs_address_1": "2402 W RIVIERA DR",
        "situs_address_2": null,
        "situs_city_state_zip": "TEMPE AZ 85282",
        "owner_city": null,
        "owner_state": null,
        "owner_zip": null,
        "situs_city": null,
        "situs_state": null,
        "situs_zip": null,
        "penalty_amount": null,
        "penalty_mail_date": null,
        "penalty_appeal_recieved_date": null,
        "penalty_bos_meeting_date": null,
        "penalty_determination_notice_sent_date": null,
        "penalty_determination": null
    }],
    "comment_count": "9"
}

数组中的第一个索引是0,而不是1。您的parcel数组中只有一个条目:result.parcel[0] 。如果你想要它的date_appeal_received属性,那就是:

console.log(result.parcel[0].date_appeal_received);

但是您的success函数以以下代码开头:

$('#ViewComments').val('View ' + result.comment_count + ' Comments');
if (result.parcel[0].apn != null){ $('#ParcelNumber').html(result.parcel[0].apn)
} else { $('#ParcelNumber').html(" "); }
console.log(result.parcel[1].date_appeal_received); // <=== Problem line

上面的"问题线"周围没有任何防护装置,因此它将始终运行。如果result.parcel只有一个条目,则result.parcel[1] undefined,因此尝试从中读取date_appeal_received属性会导致错误。检查result.parcel.length以确保它是> 1,或者由于result.parcel中的条目是对象,您可以使用

if (result.parcel[1]) {
    // ...use result.parcel[1].whateverPropertyHere
}

如果您只是想获取与apn相同的对象(result.parcel[0](上的其他属性,请继续使用result.parcel[0]

console.log("APN: " + result.parcel[0].apn);
console.log("Date received: " + result.parcel[0].date_appeal_received);
// ...and so on

或者要保存所有键入和冗余查找:

var parcel = result.parcel[0];
console.log("APN: " + parcel.apn);
console.log("Date received: " + parcel.date_appeal_received);
// ...and so on

旁注:

这也适用于从第一个条目获取属性:

console.log(result.parcel[0]["date_appeal_received"]);

。因为在 JavaScript 中,您可以使用点表示法和文字属性名称 ( foo.bar ( 或括号表示法和字符串属性名称 ( foo["bar"] ( 访问属性。