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!
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!
Share Improve this question edited Apr 30, 2015 at 16:12 Leon Gaban asked Apr 30, 2015 at 16:10 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 1-
its the
tagObjects.tags.length
which is causing the issue, I believe – Saagar Elias Jacky Commented Apr 30, 2015 at 16:13
2 Answers
Reset to default 7It'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) {
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 ing 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 parison 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.