TypeError: Cannot read property "0" from undefined

IGr*_*tch 13 javascript runtime-error google-apps-script

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" : ""
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

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?

EDIT: If I change the while to a if statement it also works.

Kra*_*mir 4

while 增加i。所以你得到:

data[1][0]
data[2][0]
data[3][0]
...
Run Code Online (Sandbox Code Playgroud)

看起来名称与数据的任何元素都不匹配。因此, while 仍然递增,并且到达数组的末尾。我建议使用for循环。