JSON JQUERY未捕获类型错误:无法读取属性'长度'的未定义

JSON JQUERY Uncaught TypeError: Cannot read property 'length' of undefined

本文关键字:属性 读取 未定义 长度 JQUERY 类型 错误 JSON      更新时间:2024-03-29

我是编程新手。我正在尝试使用JQuery将JSON数据显示到HTML文件中。我在控制台中收到一个Uncaught TypeError: Cannot read property 'length' of undefinedresumeBuilder.js:147 education.displayresumeBuilder.js:178 (anonymous function)错误,我不知道为什么。如果你能澄清这个错误,我们将不胜感激!

这是我的简历Builder.js文件:

var eduction = {
    "schools": [
        {
            "name": "McGill University",
            "location": "Montreal, Quebec, Canada",
            "degree": "Master of Arts",
            "major": "Second Language Education",
            "url": "mcgill.ca"
        }
    ],
    "onlineCourses": [
        {
            "title": "Full Stack Web Development Course",
            "school": "Bloc.io",
            "url": "bloc.io"
        }
    ]
};
education.display = function() {
    if(education.schools.length > 0 || education.onlineCourses.length > 0) {
        for(i in education.schools) {
            $("#education").append(HTMLschoolStart);
            var formattedSchoolName = HTMLschoolName.replace("%data%",      education.schools[i].name).replace("#", education.schools[i].url);
            var formattedSchoolDegree = HTMLschoolDegree.replace("%data%",     education.schools[i].degree);
            var formattedSchoolLocation = HTMLschoolLocation.replace("%data%", education.schools[i].location);
            var formattedSchoolMajor = HTMLschoolMajor.replace("%data%", education.schools[i].major);
            $(".education-entry:last").append(formattedSchoolName + formattedSchoolDegree);
            $(".education-entry:last").append(formattedSchoolLocation);
            $(".education-entry:last").append(formattedSchoolMajor);
        }
        if(education.onlineCourses.length > 0) {
            $("#education").append(HTMLonlineClasses);
            for(i in education.onlineCourses) {
                $("#education").append(HTMLschoolStart);
                var formattedOnlineTitle = HTMLonlineTitle.replace("%data%", education.onlineCourses[i].title).replace("#", education.onlineCourses[i].url);
                var formattedOnlineSchool = HTMLonlineSchool.replace("%data%", education.onlineCourses[i].school);
                var formattedOnlineURL = HTMLonlineURL.replace("%data%", education.onlineCourses[i].url).replace("#", education.onlineCourses[i].url);
                $(".education-entry:last").append(formattedOnlineTitle);
                $(".education-entry:last").append(formattedOnlineSchool);
                $(".education-entry:last").append(formattedOnlineURL);
            }
        }
    }
}
education.display();

这是我的helper.js文件:

var HTMLschoolStart = '<div class="education-entry"></div>';
var HTMLschoolName = '<a href="#">%data%';
var HTMLschoolDegree = ' -- %data%</a>';
var HTMLschoolLocation = '<div class="location-text">%data%</div>';
var HTMLschoolMajor = '<em><br>Major: %data%</em>';
var HTMLonlineClasses = '<h3>Online Classes</h3>';
var HTMLonlineTitle = '<a href="#">%data%';
var HTMLonlineSchool = ' - %data%</a>';
var HTMLonlineURL = '<br><a href="#">%data%</a>';

您的第一个JSON对象中似乎有一个拼写错误。

var eduction = {

应该有一个"a"。

您错过了教育-教育第1行

您的var eduction = {=>var education = { 中有一个打字错误

您可能还想更改:

var HTMLonlineClasses = '<h3>Online Classes</h3>';

以匹配您在javascript中比较的变量。

education.onlineCourses
var HTMLonlineCourses = '<h3>Online Courses</h3>';

因此,在未来,查找和更新对该变量的引用将更容易