Troubles with the Record Collection

Hi there, I started with javascript a while ago, and am now going back to the start of the course to refresh everything, and I’m finding myself having trouble with the record collection task. This is the error message I’m getting and below is the function (not the setup). Thanks in advance!

Error Message

Cannot read property '5439' of undefined

Cannot read property '5439' of undefined

Cannot read property '1245' of undefined

Cannot read property '2468' of undefined

Cannot read property '1245' of undefined

My Solution

function updateRecords(id, prop, value) {
  if (value === "") {
    delete collection[id][prop];
  }

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

 else {
   return "You didn't use the function correctly!";
 }
}

could you supply a link to the challenge please? will have a look

Unless you hit the last else statement, you are never returning anything from your function,
or te be more precise you are implicitly returning undefined;
hence all the error above where the program
cannot read property xxx of undefined.

You should always return the collection object at the end of your function :slight_smile:

1 Like

Thanks a lot @Marmiz. This solved my problem and the function now works perfectly fine!