javascript 类型错误:“未定义”不是对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17716070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:19:12  来源:igfitidea点击:

TypeError: 'undefined' is not an object

javascript

提问by user1483482

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:

我有一个目前相当不正常的 Javascript 程序,它一直给我带来问题。但是,它引发了一个我不明白的错误:

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

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

正如您可能猜到的那样,我正在尝试做的是检查dict 中length某个 " from" 数组的sub。这是整个函数源代码,这是我认为导致错误的循环代码:

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 TypeErrorabout something that I've already explicitly checked the type of (typeof(sub.from))...

任何想法都会很棒。坦率地说,我只是不明白为什么我会得到一个TypeError关于我已经明确检查过 ( typeof(sub.from))类型的东西......

采纳答案by HMR

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.from) !== 'undefined' && sub.from.length) {

[update]

[更新]

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

我看到您重置 sub 从而重置 sub.from 但无法重新检查 sub.from 是否存在:

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).

我的猜测是错误不在 if 语句上,而是在for(i...语句上。在 Firebug 中,您可以在出现错误时自动中断,我想它会在该行(而不是 if 语句)上中断。