Build a Library: Cannot read property 'push' of undefined at Book.addRating

Hello, my main JS has come up with this error message and I have looked through my code and I am still not sure how to solve my issue. would it be possible for someone to assist me on this matter.

i have posted my error message and code below for you to go through. :slightly_smiling_face:

/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:35
this.rating.push(value);
^

TypeError: Cannot read property ‘push’ of undefined
at Book.addRating (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:35:16)
at Object. (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:73:21)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

class media {

constructor (title) {

this._title = title;

this._isCheckedOut = false;

this._rating = [];

}

get title() {

this._title;

}

get isCheckedOut() {

this._isCheckedOut;

}

get rating() {

this._rating;

}

set isCheckedOut(value) {

this._isCheckedOut = value;

}

toggleCheckedOutStatus() {

this.isCheckedOut = !this.isCheckedOut;

}

getAverageRating() {

let ratingSum = 

this.rating.reduce((accumulator, rating) => accumulator + rating);

return ratingSum / this.rating.length

}

addRating(value) {

this.rating.push(value);

}

}

class Book extends media {

constructor(author, title, pages) {

  super(title)

  this.author

  this.pages

}

get author() {

 return this._author;

}

get pages() {

 return this._pages;

}

}

class movie extends media {

constructor(director, title, runTime) {

  super(title)

  this._director = director;

  this._runTime = runTime;

}



get director() {

  return this._director;

}

get runTime() {

  return this._runTime;

}

}

const historyOfEverything = new Book(‘Bill Bryson’, ‘A Short History of Nearly Everything’, 544);

historyOfEverything.toggleCheckedOutStatus();

console.log(historyOfEverything.isCheckedOut);

historyOfEverything.addRating(4);

historyOfEverything.addRating(5);

historyOfEverything.addRating(5);

console.log(historyOfEverything.getAverageRating())

Hey @spurz3412
what I can already tell you is that you forgot the underscore at this.rating.push(value). You should also do something with the function body of some of your getters: Either return or log the values.

1 Like

thank you very much, this really helped. :slightly_smiling_face: