Record Collection - TypeError: Cannot set property 'prop' of undefined

Hey folks, running into an issue with this. Line 25 ( records.id.prop = value; )is throwing an ‘Uncaught TypeError: cannot set property ‘prop’ of undefined’ error in both FCC and Codepen.

What am I doing wrong?

In the example below I’ve stripped away everything except the line that’s causing the issue. I think there’s a fault with how I’m trying to create and assign the new property in the recordCollection/record object.

// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'   
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
    
  records.id.prop = value;
    
}

recordCollection = updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Try records[id][prop] = value.

What ansell said. Because variable names can’t be numbers, you’re not able to access records.id when id is a number. Using brackets ( as in records[id]) doesn’t pose the variable name restrictions on id, and therefore works.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.