Record Collection "cannot read property push of undefined"

Tell us what’s happening:

getting error “Cannot read property ‘push’ of undefined”

I have read through several forum posts on this same issue and I understand that I need to declare the “tracks” property somehow. I need to declare it as empty before I can push to it, correct?

I am stuck on how to declare tracks property before I can .push

Your code so far


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  
    if (prop !== "tracks" && value !== ""){
    collection[id][prop] = value;
  }  else if (value == "") {
        delete collection[id][prop];
    } 

    if (prop === "tracks" && value !== "")  {
      collection[id][prop].push(value);
    }
     
  return collection;
     }

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.113 Safari/537.36 Vivaldi/2.1.1337.51.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/

As a hint:

var iAmAnEmptyArray = []

Thank you for the quick clue. I do not know how to declare var in the if statement.

I tried declaring this

var collection[id][prop] = []

just above the function line function updateRecords(id, prop, value)

but that did not work either.

That was just an example of an empty array, you don’t assign things to objects with var.

You already have the equivalent of var, it’s the key tracks. You maybe want to review earlier lessons on how you assign values to objects. You already have literally all that’s needed in the code you just posted, you’ve just added var which, if you can recall how to assign values to objects, doesn’t make sense: if that’s the only thing you’re having an issue with you have to have properly assigned values to the record collection in your existing code

It’s been a week and I still can not figure out this solution. I can make senes of the code I’ve pasted above but I am getting

Cannot read property 'push' of undefined

with a single gray X. I have read over a dozen posts on this forum regarding this same question and I see that many have used the hasOwnProperty attribute but I am not sure how this works. I want to clearly understand this before moving on to the next exercises.

You have a collection of records.
Each record has an id.
If record has a set of properties (prop) - album name, artist etc. Each of those properties has a value.

So you look in the collection for a record of id, and you want to update prop with value. If that property isn’t there, then you create it and set the value.

That is simple enough for all but one of the properties collection[id][prop] = value says set the prop on the record of ID id to value. If the prop isn’t there, it’ll get created. If it is there, the value will get updated.

That other property is “tracks” and the above doesn’t work because “tracks” is an array. You can push to update the array, but if the prop “tracks” does not exist, you need to create the array.

You check if a property exists by using the hasOwnProperty function. It works like myObject.hasOwnProperty(someProperty): so if myObject has the property someProperty that whole function returns true other wise false

thank you for your help! I finally got it to work. Am I allowed to paste my solution here?