jQuery 未捕获的类型错误:无法读取未定义的属性“名称”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17697412/
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-08-26 19:46:32  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'name' of undefined

javascriptjquery

提问by Chud37

I have the following code for when 'choose file' is clicked:

单击“选择文件”时,我有以下代码:

$(':file').change(function () {

if(this.files.length == 1) {
    $('#selected_files').html("<h4>Attaching " + this.files.length + " file</h4>");
} else {
    $('#selected_files').html("<h4>Attaching " + this.files.length + " files</h4>");
}

$('#selected_files').append("<table class=\"altShaded\"><thead><tr><td></td><td>Filename</td><td>Size</td></tr></thead>");

for(x=0;x<=this.files.length;x++)
{
    var file = this.files[x], 
    name = file.name, 
    size = file.size, 
    type = file.type;
    $('#selected_files').append("<tr><td></td><td><b>" + name + "</b> ("+filesize(size)+") " + type + "<br>");
}


});

Fine, right? And all works well. That's great, except that when jQuery appends the table rows, it seems to like to start a new table, and the top <thead>isnt attached the to rows (in Chrome).

好吧,对吧?一切正常。这很好,除了当 jQuery 附加表行时,它似乎喜欢启动一个新表,并且顶部没有<thead>附加到行(在 Chrome 中)。

Okay I thought, we'll just build a string and put it all in at once.

好吧,我想,我们只需构建一个字符串并立即将其全部放入。

Thus:

因此:

$(':file').change(function () {

        if(this.files.length == 1) {
            var displayFiles = "<h4>Attaching " + this.files.length + " file</h4>";
        } else {
            var displayFiles = "<h4>Attaching " + this.files.length + " files</h4>";
        }


        var displayFiles = displayFiles + "<table class=\"altShaded\"><thead><tr><td></td><td>Filename</td><td>Size</td></tr></thead>";
        for(x=0;x<=this.files.length;x++)
        {
            var file = this.files[x], 
            name = file.name, 
            size = file.size, 
            type = file.type;
            displayFiles = displayFiles + "<tr><td>" + type + "</td><td><b>" + name + "</b></td><td>"+filesize(size)+"</td></tr>";
        }

        $('#selected_files').html(displayFiles);
    });

But now all of a sudden, I get the following error:

但现在突然间,我收到以下错误:

*Uncaught TypeError: Cannot read property 'name' of undefined *

*未捕获的类型错误:无法读取未定义的属性“名称”*

Nothing has changed, except the code around it. It is pointing to:

除了周围的代码外,什么都没有改变。它指向:

name = file.name,

名称 = 文件名,

Can you tell me what the problem is here?

你能告诉我这里有什么问题吗?

采纳答案by Malharhak

This type of error mean that your container variable fileis not defined.

这种类型的错误意味着您的容器变量file未定义。

You should use console.logat different places to see what is defined and what is not (your files array, etc.)

您应该console.log在不同的地方使用以查看定义的内容和未定义的内容(您的文件数组等)

Also :

还 :

for(x=0;x<=this.files.length;x++)

Will be undefined for the last xvalue, because the last element of an array is at array.length - 1and not array.length, that gives you an undefined value at the end of your loop, probably the source of your error. In your case, x goes to the value this.files.lengthAlso, always use var, otherwise your xwill be a global variable, which can be another source of problems.

对于最后一个x值将是未定义的,因为数组的最后一个元素是 atarray.length - 1而不是array.length,这在循环结束时为您提供了一个未定义的值,这可能是错误的根源。在您的情况下, x 转到值this.files.length此外,始终使用 var,否则您x将成为全局变量,这可能是另一个问题根源。

A correct loop should be :

正确的循环应该是:

for (var x = 0; x < this.files.length; x++)

回答by DayaBM

Here is the code with the problem,

这是有问题的代码,

for (var index = 0; index <= results.length; index++) {
//doSumthing
}

Working code ,

工作代码,

for (var index = 0; index < results.length; index++) {
//doSumthing
}

The problem was with the =operator in forloop statement. It was checking for the element that does not exist(last element+1) in the array.

问题=在于for循环语句中的运算符。它正在检查数组中不存在的元素(最后一个元素+1)。

Try removing =from your loop statement, like this.

尝试=从循环语句中删除,就像这样。

for(var x = 0; x < this.files.length; x++){}

It might work!

它可能会起作用!

回答by Zohab Ali

I was having same error and it was happening because of a stupid mistake. I removed on module from imports array and accidentally left comma on that line.....so there were two commas (,,) in imports: [] array....after removing one comma it solved the problem.

我遇到了同样的错误,这是由于一个愚蠢的错误而发生的。我从导入数组中删除了模块,不小心在该行上留下了逗号……所以导入中有两个逗号 (,,):[] 数组……删除一个逗号后,它解决了问题。