19

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 Google Apps Script. 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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
IGratch
  • 301
  • 1
  • 3
  • 7
  • is data an array that is 1-based? Normally they start at index 0... – rene Sep 22 '13 at 18:55
  • 2
    you should check the parent array dimension for existence before trying to use the child dimension, `if(typeof(data[i])!=="undefined")` and prevent the while loop from going out of the array bounds `data[i].length` will give the number of elements in it. – Patrick Evans Sep 22 '13 at 18:57
  • 4
    You're incrementing `i` with no regard to bounds. – user2736012 Sep 22 '13 at 18:57

5 Answers5

4

The while increments the i. So you get:

data[1][0]
data[2][0]
data[3][0]
...

It looks like name doesn'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 for loop.

Krasimir
  • 13,306
  • 3
  • 40
  • 55
4

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

Ammend your while statement to this...

for(var iIndex = 1; iIndex <= data.length; iIndex++){
    if (data[iIndex][0] === name){
         i = iIndex;
         break;
    };
    Logger.log(data[iIndex][0]);
 };
Justin Russo
  • 2,214
  • 1
  • 22
  • 26
2

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.

JavaScript does a poor debug message. In your case, I reckon probably the access gets out of bound.

Daniel C. Deng
  • 1,573
  • 13
  • 8
-1

For me, the problem was I was using a package that isn't included in package.json nor installed.

import { ToastrService } from 'ngx-toastr';

So when the compiler tried to compile this, it threw an error.

(I installed it locally, and when running a build on an external server the error was thrown)

Erez Shlomo
  • 2,124
  • 2
  • 14
  • 27
-5

Under normal circumstances,out of bound of array when you encounter the error. So,check uo your array subscript.

Lee Ran
  • 11
  • 4
  • 6
    This can be a comment. Answer should be more explained with some codes/links/explanations. – Roxx Sep 11 '17 at 06:59