Record Collection, push of undefined

Record Collection, push of undefined
0

#1

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/


#2

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


#3

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.


#4

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’.


#5

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


#6

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)?


#7

oh of course! Thank you!


#8

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


#9

yes, should of noticed it myself. Thank you.


#10

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;
}

#11

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.


#12

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.


#13

Thanks!

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


#14

You are on the right track now.