0

enter image description here

Obviously tagObjects is an array and has a length of 0. However it's still getting past that check and causes an error.

TypeError: Cannot read property 'length' of undefined

if (tagObjects != "empty" || tagObjects.length === 0) { for (var i = 0; i < tagObjects.tags.length; i++) { temp_tags.push(tagObjects[i].tags); } temp_tags.forEach(function(obj) { if (obj.selected) { if (vs.tags[map[obj.term]] != undefined) { vs.tags[map[obj.term]].selected = true; } } }); } 

It's even getting past the string check!

enter image description here

1
  • its the tagObjects.tags.length which is causing the issue, I believe Commented Apr 30, 2015 at 16:13

2 Answers 2

7

It's an OR condition

if (tagObjects != "empty" || tagObjects.length === 0) { 

If it's not the string "empty" OR if it has no length, continue.

In other words the length doesn't matter if the Array is not the string "empty", which it probably always is if it's an array ?

Also, you're accessing tagObjects.tags which suggest it's neither a string nor an array, but an object, and objects don't have length.

In other words, your if condition makes no sense at all ?

if (tagObjects != "empty" && tagObjects.length === 0) { 
Sign up to request clarification or add additional context in comments.

Comments

2

You're making sure tagObjects exists, but it doesn't have a tagObjects.tags property (which you use on the second line). That's probably where the error is coming from.

You should change the initial condition to use something like:

if (tagObjects !== 'empty' && tagObjects.tags && tagObjects.tags.length > 0) { 

This also changes the string comparison to be strict (tagObjects must be the exact string empty without coercion) and the condition from OR (any one must be true) to AND (all must be true).

The result is a condition that checks to make sure tagObjects is not the string 'empty' and has a defined property tags with a length greater than 0.

3 Comments

Thanks! +1 Now I need to get past another problem hehe, tagsObjects[i].tags looks like I need another for loop to separate out tags.
@LeonGaban - You can't iterate over tagObjects.tags and then access tagObjects[i], it just doesn't make any sense
Yeah, making another array to separate out the tags, then do this check. Thanks for helping with the 1st problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.