Record Collection "push is undefined"?

Tell us what’s happening:
I think I have it mostly solved, but I keep getting this error: “Cannot read property ‘push’ of undefined”.
Any help would be much appreciated.

Mark

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 !== "") {
    if (collection[id][prop] !== "") {
    collection[id][prop].push(value);
    } else {
      collection[id][prop] = [value];
    }
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }
  return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

the error is trying to tell you that you cannot push something onto nothing.
so the tricky part is to figure out where that is happening.
one way to check is to add console.log statements and review the browser’s console to see what is happening as your function runs…

1 Like

ps. you may want to re-read the instructions . You forgot to implement one vital part…

1 Like

OK, Thanks hbar1st! I will try that.

@hbar1st hinted very good, but in case you may need more hint

This is becasue undefined is not "" so it enters the if.

Hint: you may assume if collection[id][prop] is there, so it’s an array.

I really think this challenge is too hard, based on the previous skills taught. We go from adding one property to an object, to a complex function with multiple if statements. Nowhere previously was I taught how to refer to multiple-level nested properties in an object. There are just too many things that could be wrong here.

true, but there isn’t. you’ve done everything quite well. just missed one request in the challenge…

Well this is what I have now:

// Only change code below this line
function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
    if (collection[id].hasOwnProperty("tracks" === true)) {
    collection[id][prop] = value;
    } else {
      collection[id][prop] = value;
    }
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }
  return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Yes, but not using parameters in a function.

what’s going on here?

hasOwnProperty takes a string, not a boolean…

what happened to your push statement?

also, if “tracks” is not a property, why do you still say:
collection[id][prop] = value. Remember, at this point, tracks is not a property (ie. once you fix your call to hasOwnProperty that is)

Yeah, well, I clearly do not understand this. I went over all the lessons several times. I just cannot get it.

no need to throw in the towel. You’ve done great so far…

Here’s the section of the instructions I was trying to get you to re-read:
There are several rules for handling incomplete data:

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

===============
notice they said that if the album doesn’t have the tracks property, then create an empty array?
That’s the part you were missing.
But now also you are missing the second part about the push…

1 Like

But how do I create an empty array?

I figured it out. Thanks for your help, hbar1st!

1 Like