Please help Basic JavaScript: Record Collection

I thought I had an idea of what I was doing, but
I keep getting the following error:

TypeError: Cannot read property ‘push’ of undefined

when I enter the following code:

function updateRecords(id, prop, value) {
if (value.length == 0){
delete collection[id][prop]
}

else {
collection[id][prop].push[value];
}

return collection;

}

// 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”
}
};

// Only change code below this line
function updateRecords(id, prop, value) {
if (value.length == 0){
delete collection[id][prop]
}
else {
collection[id][prop].push[value];
}
return collection;
}

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



**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36</code>.

**Challenge:** Record Collection

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

here you have a syntax error, don’t know if it is the source of the error you are getting
remember that push is a function

after that you will get some other errors - if you need help with that ask again

1 Like

I just realized I submitted the .push[value] into the forum

but I’ve also tried it with .push(value)

function updateRecords(id, prop, value) {
if (value.length == 0){
delete collection[id][prop]
}
else {
collection[id][prop].push(value);
}
return collection;
}

This is basically what I’m asking
why does the following(equals statement) work:

function updateRecords(id, prop, value) {
if (value.length == 0){
collection[id][prop].push(value);
}
return collection;
}

but the following (not equals) returns the
TypeError: Cannot read property ‘push’ of undefined:

function updateRecords(id, prop, value) {
if (value.length !== 0){
collection[id][prop].push(value);
}
return collection;
}

remember this is your function call:

and that the object is made like this:

there is no artist property. collection[id][prop] is undefined
push is an array method, you can’t use it on an undefined value (and neither on a string).

1 Like

in this one the part inside the if statement is not executed so you don’t get the error

1 Like

Thank you. I see what you’re saying
Thank you so much.

I’m still going to play around with it though.

@ilenia ieahleen ieahleen!

I’m getting there. Thank you so much.

function updateRecords(id, prop, value) {
if (value == “”){
delete collection.id.prop;
}
else if (value !== “”){
collection[id][prop] = (value)
}
return collection;
}

You have to retype the quotations, but surprisingly that solved it completely without mentioning “tracks”

I also have to thank @SJ50 for the idea in the hint thread

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Ok, thank you I see now.

I must have had “tracks” in the code somewhere because I tested it again
and it didn’t work without “tracks”.

I watched the video in order to add the following

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

Once again thank you

do you understand what this does?

1 Like

Not exactly.
Thank you for asking.
Perhaps it means the following

the object id property is itself
or the object id property is an array

So, it can replace a boolean such as;

if (x == true){
return "y";
}
else if (x == false){
 return "z";
}

?

Well, I just looked it up and I see that || doesn’t mean “or”.

It’s a logical or

credit: stackoverflow.com

Thank you @camperextraordinaire and @ilenia.