Record Collection "push is undefined"?

Record Collection "push is undefined"?
0.0 0

#1

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


#2

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…


#3

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


#4

OK, Thanks hbar1st! I will try that.


#5

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


#6

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.


#7

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


#8

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");

#9

Actually, two lessons back in the Basic JavaScript section, there was a lesson named Accessing Nested Objects, where you were taught about nested objects.


#10

Yes, but not using parameters in a function.


#11

Not sure what you mean.


#12

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)


#13

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


#14

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…


#15

Did you by chance mean you never were shown how to reference an object property value using a variable? For example, if you were given the following code:

var myObject = {
  prop1: 'value1',
  prop2: 'value2'
};

And then given another variable containing the name of one of the myObject’s properties:

var propName = 'prop2';

How you could then get ‘prop2’ value of myObject by using bracket notation like:

var prop2Value = myObject[promName];  // assigns 'value2' to prop2Value

This was covered in another previous lesson entitled Accessing Object Properties with Variables.


#16

But how do I create an empty array?


#17

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