如何解决 TypeError: Cannot read property 'push' of undefined in simple node project?

Posted

技术标签:

【中文标题】如何解决 TypeError: Cannot read property \'push\' of undefined in simple node project?【英文标题】:how to solve TypeError: Cannot read property 'push' of undefined in simple node project?如何解决 TypeError: Cannot read property 'push' of undefined in simple node project? 【发布时间】:2019-08-05 11:49:35 【问题描述】:

我正在尝试创建一个简单的通讯录,

//globol object for contacts database
// Business Logic for AddressBook ---------
function AddressBook(contacts) 
    this.contacts = [];


AddressBook.prototype.addContact = (contact) => 
    this.contacts.push(contact);


// Business Logic for Contacts ---------
function Contact(firstName, lastName, phoneNumber) 
    this.firstName = firstName,
        this.lastName = lastName,
        this.phoneNumber = phoneNumber


Contact.prototype.fullName = () => 
    return this.firstName + " " + this.lastName;


var addressBook = new AddressBook();
var contact = new Contact("Ada", "Lovelace", "503-555-0100");
var contact2 = new Contact("Grace", "Hopper", "503-555-0199");
addressBook.addContact(contact);
addressBook.addContact(contact2);

console.log(addressBook.contacts);

但它会打印错误

.../addressBook.js:8 this.contacts.push(contact); ^

TypeError:无法读取未定义的属性“推送” 在 AddressBook.addContact (/home/maku/Documents/epicodus/addressBook.js:8:19) 在对象。 (/home/maku/Documents/epicodus/addressBook.js:25:13) 在 Module._compile (internal/modules/cjs/loader.js:701:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) 在 Module.load (internal/modules/cjs/loader.js:600:32) 在 tryModuleLoad (internal/modules/cjs/loader.js:539:12) 在 Function.Module._load (internal/modules/cjs/loader.js:531:3) 在 Function.Module.runMain (internal/modules/cjs/loader.js:754:12) 启动时(内部/bootstrap/node.js:283:19) 在 bootstrapNodeJSCore (内部/bootstrap/node.js:622

【问题讨论】:

【参考方案1】:

不要使用箭头函数check this

如果您使用箭头函数,this 将不会绑定到当前实例。您需要使用关键字function 创建函数:

function AddressBook(contacts) 
    this.contacts = [];


AddressBook.prototype.addContact = function(contact) 
    this.contacts.push(contact);


// Business Logic for Contacts ---------
function Contact(firstName, lastName, phoneNumber) 
    this.firstName = firstName,
        this.lastName = lastName,
        this.phoneNumber = phoneNumber


Contact.prototype.fullName = function() 
    return this.firstName + " " + this.lastName;


var addressBook = new AddressBook();
var contact = new Contact("Ada", "Lovelace", "503-555-0100");
var contact2 = new Contact("Grace", "Hopper", "503-555-0199");
addressBook.addContact(contact);
addressBook.addContact(contact2);

console.log(addressBook.contacts);

【讨论】:

不过,你怎么知道什么时候使用箭头函数? Rahul Sharma 当您不需要将this 绑定到当前范围时,您不想使用箭头函数。【参考方案2】:

箭头函数改变了'this'关键字的范围,在你的addContact方法中,将方法更改为函数声明,它将消除错误

【讨论】:

以上是关于如何解决 TypeError: Cannot read property 'push' of undefined in simple node project?的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: cannot use a string pattern on a bytes-like object

我该如何解决这个错误 TypeError: Cannot read properties of null (reading 'indexOf') in my redux application

× TypeError: Cannot read properties of undefined (reading 'getState')

A* 算法 TypeError: cannot unpack non-iterable int object

已解决TypeError: Descriptors cannot not be created directly.

Nodejs报内部错误 TypeError: Cannot read property ‘destroy‘ of undefined的解决方法