Record Collection "collection[id][prop].push is not a function"

Record Collection "collection[id][prop].push is not a function"
0

#1

Tell us what’s happening:
Hey everyone. I’ve been stuck on this for a bit now. I have tried solving this in many different ways but always come to the same result. I have one unchecked bullet point and keep getting the same error message over and over again.

The error message is:
“collection[id][prop].push is not a function”

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

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

I have tried a different approach:

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

This time I got the following error: “Cannot read property ‘push’ of undefined”

The unchecked task, however is still the same one.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

For the first one, the second of these two lines is the problem:

collection[id][prop] = [];
collection[id][prop] = value;

The first line is fine, but then you overwrite that with value, which is a string, then you try to push to that.

The second one, regardless of if there is a tracks array present or not, you try to push the value, so again, it fails.


#3

Thank you soo much. I just had to remove that line and it worked perfectly.