javascript 类型错误:无法从未定义中读取属性“0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18947556/
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
TypeError: Cannot read property "0" from undefined
提问by IGratch
I'm getting a very weird undefined error:
我收到一个非常奇怪的未定义错误:
function login(name,pass) {
var blob = Utilities.newBlob(pass);
var passwordencode = Utilities.base64Encode(blob.getBytes());
var ss = SpreadsheetApp.openById("");
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange().getValues();
var i=1;
while (name != data[i][0]){
Logger.log(data[i][0]);
i++;
}
if (passwordencode == data[i][1]){
UserProperties.setProperties({
"name" :name,
"pass" : passwordencode
});
Logger.log("You are logged in");
}
else if (passwordencode != data[i][1]) {
Logger.log("You are not logged in");
UserProperties.setProperties({
"name" : "",
"pass" : ""
});
}
}
using googlescript. The one that's undefined is the while statement where while(name != data[i][0]) claiming that you cannot read property "0" from undefined. What's weird about this, If I remove the data[i][0] in the while statement, it still works in the logger.log. And everywhere else. What the heck is going on?
使用谷歌脚本。未定义的是 while 语句,其中 while(name != data[i][0]) 声称您无法从 undefined 中读取属性“0”。这有什么奇怪的,如果我删除 while 语句中的 data[i][0] ,它仍然可以在 logger.log 中工作。还有其他地方。到底他妈发生了什么?
EDIT: If I change the while to a if statement it also works.
编辑:如果我将 while 更改为 if 语句,它也可以工作。
回答by Krasimir
The while increments the i. So you get:
while 增加i。所以你得到:
data[1][0]
data[2][0]
data[3][0]
...
It looks like namedoesn't match any of the the elements of data. So, the while still increments and you reach the end of the array. I'll suggest to use forloop.
看起来名称与数据的任何元素都不匹配。因此,while 仍然会递增,并且您会到达数组的末尾。我建议使用for循环。
回答by Justin Russo
Looks like what you're trying to do is access property '0' of an undefined value in your 'data' array. If you look at your while statement, it appears this is happening because you are incrementing 'i' by 1 for each loop. Thus, the first time through, you will access, 'data[1]', but on the next loop, you'll access 'data[2]' and so on and so forth, regardless of the length of the array. This will cause you to eventually hit an array element which is undefined, if you never find an item in your array with property '0' which is equal to 'name'.
看起来您要做的是访问“数据”数组中未定义值的属性“0”。如果您查看您的 while 语句,就会发现这种情况正在发生,因为您为每个循环将 'i' 增加 1。因此,第一次通过时,您将访问“data[1]”,但在下一个循环中,您将访问“data[2]”,依此类推,无论数组的长度如何。这将导致您最终命中一个未定义的数组元素,如果您从未在数组中找到属性 '0' 等于 'name' 的项目。
Ammend your while statement to this...
将您的 while 语句修改为...
for(var iIndex = 1; iIndex <= data.length; iIndex++){
if (data[iIndex][0] === name){
break;
};
Logger.log(data[i][0]);
};
回答by Daniel C. Deng
Check your array index to see if it's accessed out of bound.
检查您的数组索引以查看它是否被越界访问。
Once I accessed categories[0]. Later I changed the array name from categories to category but forgot to change the access point--from categories[0] to category[0], thus I also get this error.
一旦我访问了类别[0]。后来我将数组名称从类别更改为类别,但忘记更改访问点 - 从类别 [0] 到类别 [0],因此我也收到此错误。
JavaScript does a poor debug message. In your case, I reckon probably the access gets out of bound.
JavaScript 的调试信息很差。在您的情况下,我认为访问权限可能超出范围。
回答by Lee Ran
Under normal circumstances,out of bound of array when you encounter the error. So,check uo your array subscript.
一般情况下,遇到错误时越界。所以,检查你的数组下标。