Basic Javascript: Record Collection - Cannot read property 'prop' of undefined

Hi all,

This is the function I have written for the Basic Javascript module exercise ‘Record Collection’ (https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection)

I keep receiving the error message Cannot read property 'prop of undefined' of undefined and cannot figure out why.

(This function that I coded below occupies lines 31 - 49 in the Freecodecamp internal text editor, seen on this webpage: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection)

// Only change code below this line
function updateRecords(id, prop, value) {
  var newID = id.toString(); 
  console.log(prop);
  if (prop !== 'tracks' && collection.newID.prop === false ) {
    collection.newID = prop;
    collection.newID.prop = value; 
  }  else if (prop !== 'tracks' && collection.newID.prop.value !=='') { 
    collection.newID.prop = value; 
  } else if (prop === 'tracks' && collection.newID.tracks == '') {
      collection.newID.prop = [];
      collection.newID.prop.push(value);
  }  else if (prop === 'tracks' && collection.newID.tracks.isArray) {
      collection.newID.prop.push(value);     
  }  else if (collection.newID.value === '') {
        delete collection.newID.prop;

    }
  
  return collection

Sorry if this has been asked before - I am looking through similar topics and cannot find a post that mirror this one, mostly because I do not know enough about the problem to know what to search for when looking at other FCC posts about this exercise.

Before posting this I tried:

  1. using console.log(newID) to make sure that newID was converting the number to a string before Javascript function applied that argument to the object.

  2. coded:

if (prop !== 'tracks' && collection.newID.prop === false ) { collection.newID = prop; collection.newID.prop = value;

to try and ensure that if prop is undefined (as per the error message noted above) then the function would assign a new prop key within the object and then assign the function’s value to that key. But this doesn’t work. After I coded this in and ran the function I still received the error message `Cannot read property ‘prop of undefined’

Thanks for reading!

Take a closer look at the following 3 lessons
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables

Hi,

I have just done so. I cannot see how this error relates to these lessons?

Why are you turning id into id.toString()?

To access props you need to check indexes, not write it as collection.newID.tracks.
Example, to look up the id in a the collection you should go by collection[id] to get the matching object-index by id, then accessing it’s props with [props], which you can do directly like this, collection[id][props].

Try and see if it makes things clearer.

As you have rechecked those challenges, what do you think these are?

collection.newID

collection.newID.prop

Hi @Ronnehag

I have to turn id into id.toString() because the test that freecodecamp runs on your code (check the script on the webpage https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection) is:

updateRecords(5439, "artist", "ABBA");

Notice 5438 is number, whereas the actual object I am working on contains keys that are strings - not numbers. Please click the link and take a look.

Also, I cannot access this object by index - it is not an array, and so therefore can only be accessed via keys such as collection.newID.prop

Please correct me if I am wrong - also please do click the link to see what I mean.

I am pasting it here again: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Hi @ilenia sorry but I do not understand your response. May you please elaborate on your point?

You need to go back and read the challenge on bracket notation.

You can “index”, but it is usually referred to as a “key”, not an index. That is why an object is often called an associative array.

Property accessors

One can think of an object as an associative array (a.k.a. map, dictionary, hash, lookup table). The keys in this array are the names of the object’s properties.

You may also want to give the for…in loop a look, it may provide some clues.

Dot notation search literally, collection.newID doesn’t work because you don’t have a key that is newID, instead, bracket notation…

Hi @leahleen

The test is

updateRecords(5439, "artist", "ABBA");

Now my function reads:

var newID = id.toString(); 
console.log(prop);
  if (prop !== 'tracks' && collection.newID.prop === false ) {
...

So NewID is used to access the array’s index number(in this case i.e. the test, 5439, which is coded in the array as a string, ‘5439’ - hence id.toString(); )

I don’t understand your feedback I am afraid. Could you please clarify?

Also, Hi @lasjorg,

I have read the challenge on bracket notation again. I can access object keys via dot notation, which is what I have used. So why should I use bracket notation instead?

I am confused by your feedback too.

Thanks again all

This is undefined, because dot notation access literally a key newID which doesn’t exist. Bracket notation evaluated what’s inside the brackets before accessing the property.

id, prop and value are variables not properties on the object.

When you do this “collection.newID” you are looking on the collection object for a property called newID, not the value of the newID variable.

I would suggest you break this down into a smaller example, paste it in the browser console and log out the results.

var collection = {
  "2548": {
    "album": "Slippery When Wet",
    "artist": "Bon Jovi",
    "tracks": [ 
      "Let It Rock", 
      "You Give Love a Bad Name" 
    ]
  },
};

collection.newID
undefined

collection['2548']
{album: "Slippery When Wet", artist: "Bon Jovi", tracks: Array(2)}

const newID = 2548;
collection[newID]
{album: "Slippery When Wet", artist: "Bon Jovi", tracks: Array(2)}