Issue:
I am having issues employing bracket notation as well as dot notation for accessing properties of an object for my Lab. I included various debugging messages in my code to track where my program is stopping and determining why its throwing the following error message:
TypeError: Cannot read properties of undefined (reading 'tracks')
My program is NOWHERE NEAR finished, as I’m simply laying out a foundation. I realize there are likely many syntax/logical errors in it’s current state. However, I am unable to progress any further because I can’t figure out why I am unable to access the tracks property using dot notation OR using bracket notation with the id variable instead of a string literal. An example of this issue is shown in the debugging messages.
MY CODE:
const 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'
}
};
function updateRecords(records, id, prop, value) {
console.log(records["5439"]["albumTitle"]); // Bracket Notation here works fine
console.log("Checkpoint 0");
if (value === null) {
console.log("Checkpoint 1");
delete records["id"]["prop"];
}
console.log("Checkpoint A");
console.log(records["2468"]["tracks"]); // Able to access the 'tracks' property
console.log(records["id"]["tracks"]); // Throws TypeError
console.log(records.id.tracks); // Throws TypeError (Likely since id starts with a number)
// PROGRAM STOPPED HERE (Excluding the debugging messages)
if (prop !== records["id"]["tracks"] && value !== null) {
console.log("Checkpoint 2");
records["id"].prop = value;
}
console.log("Checkpoint B");
if (prop === tracks && value !== null && records["id"].hasOwnProperty("tracks") === false) {
console.log("Checkpoint 3");
const valArr = [];
valArr.push(value);
}
if (prop === tracks && value !== null) {
console.log("Checkpoint 4");
records["id"].tracks.push(value);
}
console.log("Checkpoint 5");
return records;
}
// DEBUGGING
console.log("#### PARENT OBJECT ####\n");
console.log(recordCollection);
console.log("\n#### BRACKET NOTATION TEST ####\n");
console.log(recordCollection["2548"]);
console.log(recordCollection["2548"]["albumTitle"]);
console.log("\n#### FUNCTION CALL ####\n");
console.log(updateRecords(recordCollection, 2468, "artist", ""));
CONSOLE OUTPUT | DEBUGGING
#### PARENT OBJECT ####
{ '1245': { artist: 'Robert Palmer', tracks: [] },
'2468':
{ albumTitle: '1999',
artist: 'Prince',
tracks: [ '1999', 'Little Red Corvette' ] },
'2548':
{ albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
'5439': { albumTitle: 'ABBA Gold' } }
#### BRACKET NOTATION TEST ####
{ albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] }
Slippery When Wet
#### FUNCTION CALL ####
ABBA Gold
Checkpoint 0
Checkpoint A
[ '1999', 'Little Red Corvette' ]
TypeError: Cannot read properties of undefined (reading 'tracks')
Challenge Information:
Build a Record Collection - Build a Record Collection
