Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'undefined' is not an object

Tags:

I have a currently fairly dysfunctional Javascript program that's been causing me problems. However, it throws one error that I just don't understand:

TypeError: 'undefined' is not an object (evaluating 'sub.from.length')

What I'm trying to do, as you can probably guess, is check the length of a certain "from" array in the sub dict. Here's the source code for the entire function, and here's the code of the loop that I think is causing the error:

console.log(afcHelper_ffuSubmissions.length); // just for debugging, returns the correct number
for (var i = 0; i < afcHelper_ffuSubmissions.length; i++) { // this whole section works fine
    var sub = afcHelper_ffuSubmissions[i];
    //console.log("THIS IS BROKEN DOWN BY LINK",afcHelper_Submissions[i]);
    if (pagetext.indexOf(afcHelper_ffuSections[sub.section]) == -1) {
        // Someone has modified the section in the mean time. Skip.
        document.getElementById('afcHelper_status').innerHTML += '<li>Skipping ' + sub.title + ': Cannot find section. Perhaps it was modified in the mean time?</li>';
        continue;
    }
    var text = afcHelper_ffuSections[sub.section];
    var startindex = pagetext.indexOf(afcHelper_ffuSections[sub.section]);
    var endindex = startindex + text.length;

    console.log(sub); 
    if (typeof(sub.from) != 'undefined' && sub.from.length > 0) { // ** problem spot?? this is the code i recently added.
        for (var i = 0; i < sub.from.length; i++) {
            mainid = sub.from[i]['id'];
            var sub = afcHelper_Submissions[mainid]; // and then it goes on from here...

Any ideas would be great. Frankly, I just can't see why I'm getting a TypeError about something that I've already explicitly checked the type of (typeof(sub.from))...

like image 427
user1483482 Avatar asked Jul 18 '13 06:07

user1483482


People also ask

What does undefined is not an object mean?

The “TypeError: 'undefined' is not an object” error occurs when a property is accessed or a method is called on an undefined object. This error is shown only on safari browser.

What is the reason for getting TypeError undefined is not an object?

This is a common JavaScript error that happens when you try to call a function before it is defined. You get this error when you try to execute a function that is uninitialized or improperly initialized . It means that the expression did not return a function object.

How do you fix TypeError Null is not an object?

In JavaScript , null is not an object; and won't work. You must provide a proper object in the given situation. We can resolve this type of issues by adding an event listener that will notify us when the page is ready. Once the addEventListener is fired, the init() method can make use of the DOM elements.

Is not an object JavaScript error?

The TypeError: null is not an object occurs when a property is read (or set) or a method is called on a null value. An object was expected in code but was not provided. Since null is not an object in JavaScript, using a null value when an object is expected does not work.


1 Answers

I'm not sure how you could just check if something isn't undefined and at the same time get an error that it is undefined. What browser are you using?

You could check in the following way (extra = and making length a truthy evaluation)

if (typeof sub !== 'undefined' && sub.from && sub.from.length) {

[update]

I see that you reset sub and thereby reset sub.from but fail to re check if sub.from exist:

for (var i = 0; i < sub.from.length; i++) {//<== assuming sub.from.exist
            mainid = sub.from[i]['id'];
            var sub = afcHelper_Submissions[mainid]; // <== re setting sub

My guess is that the error is not on the if statement but on the for(i... statement. In Firebug you can break automatically on an error and I guess it'll break on that line (not on the if statement).

like image 137
HMR Avatar answered Oct 24 '22 05:10

HMR