javascript 未捕获的类型错误:无法读取未定义的属性“长度”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6011731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property 'length' of undefined
提问by AnApprentice
I'm working to built a contact list that is grouped by the first letter of the contact's last name.
我正在努力构建一个按联系人姓氏的第一个字母分组的联系人列表。
After a succesfull ajax request, the contact is pushed to addContact:
成功的ajax请求后,联系人被推送到addContact:
Ajax success:
阿贾克斯成功:
ko.utils.arrayForEach(dataJS.contactList, function(c) {
contactsModel.addContact(c);
});
contactsModel.addContact:
contactsModel.addContact:
//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);
The contacts json object from the server looks like this:
来自服务器的联系人 json 对象如下所示:
{
"total_pages": 10,
"page": page,
"contactList": [{
"photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
"lname": "Bond",
"id": 241,
"fname": "James",
"email": "[email protected]"},
While this works in jsfiddle, when I try it locally, I get the following error during the first push to addContact:
虽然这在 jsfiddle 中有效,但当我在本地尝试时,在第一次推送到 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
Ideas? Thanks
想法?谢谢
回答by mVChr
Your syntax looks wrong in the for
loops and elsewhere, that is ()
meant for invoking functions. Try changing everywhere you have contactsByLetter()
and contacts()
to contactsByLetter
and contacts
.
您的语法在for
循环和其他地方看起来是错误的,这()
意味着调用函数。尝试改变无处不在,你必须contactsByLetter()
和contacts()
以contactsByLetter
和contacts
。
回答by jcreamer898
Make sure you wrap your bindings statement in a jQuery ready...
确保将 bindings 语句包装在 jQuery 中...
$(function(){
ko.applyBindings(contactsModel, document.getElementById("view-panel-contacts"));
});
That worked for me using the code you provided in the fiddle.
使用您在小提琴中提供的代码对我有用。