Record Collection - "Cannot read property 'push' of undefined"

Tell us what’s happening:

Hi guys,

I dont know it keeps giving me this error
“Cannot read property ‘push’ of undefined”
Please advice

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(value !== ""){
    if(prop !== "tracks"){
      collection[id][prop] = value;
    } else if(prop === "tracks" && collection.hasOwnProperty("tracks") === false){
      collection[id][prop].push(value);
    }
  } else if (value == ""){
    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/75.0.3770.142 Safari/537.36.

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

so this line says “If the property is ‘tracks’, and the collection doesn’t have the tracks property (not the collection[id], but the collection), then go ahead and push something onto the tracks property of collection[id].”

Do you see a problem with that sentence?

Ahh… i get what u mean, so i have updated the code to this
but there’s still an error,
the error says "After updateRecords(1245, “tracks”, “Addicted to Love”), tracks should have "
I thought i did push it to the back of the array using .push()

if(value !== ""){
    if(prop !== "tracks"){
      collection[id][prop] = value;
    } else if(prop === "tracks" && collection[id].hasOwnProperty("tracks") === false){
      collection[id][prop] = [];
      collection[id][prop].push(value);
    }
  } else if (value == ""){
    delete collection[id][prop]
  }

  return collection;
}

… And you just went from one extreme to the other. Now, you’re saying “Whether or not you have a tracks property, set it to an empty array, and push this value onto it.”

So is there a way you can check if there is a property called tracks already and, if there’s NOT, then create an empty array? Then, either way, you can simply push the new value onto that.

Hello kind sir,

Thank you for being patient to me…
so… i’ve manage to go this far, i thought i got all of it correct but there’s still an error saying:
“After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.”

here’s my code now:

function updateRecords(id, prop, value) {
  if(value !== ""){
    if(prop !== "tracks"){
      collection[id][prop] = value;
    } else if (prop === "tracks"){
      if (collection[id][prop]){
        collection[id][prop].push(value);
      } else {
        collection[id][prop] = [];
      }
    }
  } else if (value == ""){
    delete collection[id][prop]
  }

  return collection;
}

you are saying, if collection[id][prop] is truthy (in this case is like “if it is not an empty array or undefined”) then push the value to it, else create an empty array
(don’t you think there is something missing?)

1 Like

u mean and empty array with a value in it?

if you have a value in the array it is not empty anymore

i did this and it worked!

it was collection[id][prop] = [];
then i just added value in the array like so: collection[id][prop] = [value];

function updateRecords(id, prop, value) {
  if(value !== ""){
    if(prop !== "tracks"){
      collection[id][prop] = value;
    } else if (prop === "tracks"){
      if (collection[id][prop]){
        collection[id][prop].push(value);
      } else {
        collection[id][prop] = [value];
      }
    }
  } else if (value == ""){
    delete collection[id][prop]
  }

  return collection;
}
1 Like