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

Javascript 未捕获类型错误:无法读取属性';长度';未定义的,javascript,jquery,knockout.js,Javascript,Jquery,Knockout.js,我正在建立一个联系人列表,该列表按联系人姓氏的第一个字母分组 ajax请求成功后,联系人被推送到addContact: Ajax成功: ko.utils.arrayForEach(dataJS.contactList, function(c) { contactsModel.addContact(c); }); //add a contact in the right spot under the right letter contactsModel.addCo

我正在建立一个联系人列表,该列表按联系人姓氏的第一个字母分组

ajax请求成功后,联系人被推送到addContact:

Ajax成功:

    ko.utils.arrayForEach(dataJS.contactList, function(c) {
        contactsModel.addContact(c);
    });
//add a contact in the right spot under the right letter
contactsModel.addContact = function(newContact) {
    //grab first character
    var firstLetter = (newContact.lname || "").charAt(0).toUpperCase();
    //if it is a number use #
    if (!isNaN(firstLetter)) {
        firstLetter = "#";
    }

    //do we already have entries for this letter
    if (!this.letterIndex[firstLetter]) {
    //new object to track this letter's contacts
        var letterContacts = {
            letter: firstLetter,
            contacts: ko.observableArray([])
        };

        this.letterIndex[firstLetter] = letterContacts; //easy access to it

        //put the numbers at the end
        if (firstLetter === "#") {
            this.contactsByLetter.push(letterContacts);
        } else {
            //add the letter in the right spot 
            for (var i = 0, lettersLength = this.contactsByLetter().length; i < lettersLength; i++) {
                var letter = this.contactsByLetter()[i].letter;

                if (letter === "#" || firstLetter < letter) {
                     break;  
                }
            }  
            this.contactsByLetter.splice(i, 0, letterContacts);
        }
    }

    var contacts = this.letterIndex[firstLetter].contacts;

    //now we have a letter to add our contact to, but need to add it in the right spot
    var newContactName = newContact.lname + " " + newContact.fname;
    for (var j = 0, contactsLength = contacts().length; j < contactsLength; j++) {
        var contactName = contacts()[j].lName + " " + contacts()[j].fName;

        if (newContactName < contactName) {
           break;  
        }
    }

    //add the contact at the right index
    contacts.splice(j, 0, newContact);

}.bind(contactsModel);
联系人模型。添加联系人:

    ko.utils.arrayForEach(dataJS.contactList, function(c) {
        contactsModel.addContact(c);
    });
//add a contact in the right spot under the right letter
contactsModel.addContact = function(newContact) {
    //grab first character
    var firstLetter = (newContact.lname || "").charAt(0).toUpperCase();
    //if it is a number use #
    if (!isNaN(firstLetter)) {
        firstLetter = "#";
    }

    //do we already have entries for this letter
    if (!this.letterIndex[firstLetter]) {
    //new object to track this letter's contacts
        var letterContacts = {
            letter: firstLetter,
            contacts: ko.observableArray([])
        };

        this.letterIndex[firstLetter] = letterContacts; //easy access to it

        //put the numbers at the end
        if (firstLetter === "#") {
            this.contactsByLetter.push(letterContacts);
        } else {
            //add the letter in the right spot 
            for (var i = 0, lettersLength = this.contactsByLetter().length; i < lettersLength; i++) {
                var letter = this.contactsByLetter()[i].letter;

                if (letter === "#" || firstLetter < letter) {
                     break;  
                }
            }  
            this.contactsByLetter.splice(i, 0, letterContacts);
        }
    }

    var contacts = this.letterIndex[firstLetter].contacts;

    //now we have a letter to add our contact to, but need to add it in the right spot
    var newContactName = newContact.lname + " " + newContact.fname;
    for (var j = 0, contactsLength = contacts().length; j < contactsLength; j++) {
        var contactName = contacts()[j].lName + " " + contacts()[j].fName;

        if (newContactName < contactName) {
           break;  
        }
    }

    //add the contact at the right index
    contacts.splice(j, 0, newContact);

}.bind(contactsModel);
虽然这在JSFIDLE中起作用,但当我在本地尝试时,在第一次推送到addContact时,会出现以下错误:

Uncaught TypeError: Cannot read property 'length' of undefined
jQuery.jQuery.extend._Deferred.deferred.resolveWithjquery-1.5.1.js:869
donejquery-1.5.1.js:6591
jQuery.ajaxTransport.send.callbackjquery-1.5.1.js:7382

想法?谢谢

对于循环和其他地方,您的语法在
中看起来是错误的,即
()
用于调用函数。试着将你所有的
contactsByLetter()
contacts()
更改为
contactsByLetter
contacts

你的语法在
循环和其他地方看起来是错误的,这就是调用函数的
()
。尝试将您所有的
contactsByLetter()
contacts()
更改为
contactsByLetter
contacts
确保将绑定语句包装在jQuery ready中

$(function(){
    ko.applyBindings(contactsModel, document.getElementById("view-panel-contacts"));
});
使用你在小提琴中提供的代码对我有效


确保将绑定语句包装在jQuery ready中

$(function(){
    ko.applyBindings(contactsModel, document.getElementById("view-panel-contacts"));
});
使用你在小提琴中提供的代码对我有效


谢谢,但这似乎不管用。这是一把小提琴:我不明白的是,为什么它在小提琴中起作用,但对美沙克来说是错误的,但这似乎不起作用。这是一个提琴:我无法理解的是为什么它在提琴中工作,但在本地机器上,在
var contacts=this.letterIndex[firstLetter].contacts之后,我会出错执行
控制台。记录(联系人)
并确保联系人是您尝试使用的正确对象。在本地计算机上,在
var contacts=this.letterIndex[firstLetter]之后执行
console.log(contacts)
并确保contacts是您尝试使用的正确对象。