Record Collection, push of undefined

Tell us what’s happening:
line 36 throws:: cannot read property push of undefined::
I would simply like to know why that is.

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(prop === 'tracks' && !collection[id].hasOwnProperty("tracks")){  
      collection[id].tracks = [];
      collection[id][prop].tracks.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/67.0.3396.99 Safari/537.36.

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

As in the else if block you have specified the above condition, so there is no track property in collection[id][prop] object

1 Like

Also be aware that you have a typo here. 'tracks'&& should have a space. Something that I learned that is really important is keeping good indentation and your formatting consistent. It will make tracking down bugs fast.

sorry, could you be more specific? I figured that if collection.5439, has no ‘tracks’ property than it should create an empty array called tracks, if the prop passed is ‘tracks’.

thanks, made it more consistent, still the issue persists.

1 Like

Great! Now that we’re good on indentation and formatting, let’s review the prompt.

The prompt: 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.

collection[id].tracks = [];
collection[id][prop].tracks.push(value); 

The problem is in the second line. What’s the problem with it? Specifically, [prop].tracks.push(value)?

oh of course! Thank you!

1 Like

Did you get it? :smile:Let us know here if you need more help of course!

yes, should of noticed it myself. Thank you.

I’m also getting “Cannot read property ‘push’ of undefined”, even after using the author’s solution. So confused!

function updateRecords(id, prop, value)
{

if (value ==='')
{
  delete collection[id][prop];
}
else if (prop !== 'tracks')
{
  collection[id][prop] = value;
}
else 
{
  collection[id][prop].push(value);
}
return collection;
}

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

That is because your else block of code (shown below) attempts to execute.

collection[id][prop].push(value);

Since id = 5439 and prop is “tracks”, you basically doing the following:

collection["5439"]["tracks"].push("Take a Chance on Me")

However, since there is no “tracks” property in the object, you can not push a value into a non-existent array. collection[“5439”][“tracks”] is undefined, because there is no “tracks” property.

Thanks!

So, it sounds like I would need to create a “tracks” property with a blank array first. Am I right?

You are on the right track now.