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?
EDIT: If I change the while to a if statement it also works.
The “cannot read property of undefined” error occurs when you attempt to access a property or method of a variable that is undefined . You can fix it by adding an undefined check on the variable before accessing it.
To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.
Causes for TypeError: Cannot Read Property of Undefined In short, the value is not assigned. In JavaScript, properties or functions are Objects, but undefined is not an object type. If you call the function or property on such variable, you get the error in console TypeError: Cannot read undefined properties.
The "Cannot set properties of undefined" error occurs when setting a property on an undefined value. To solve the error, conditionally check if the value is of the expected type (object or array) or has to be initialized before setting the property on it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With