I'm working in an Angular app and inside a controller I need to iterate over an array of objects. This is the controller and the code involved in this situation:
myapp.controller('LoginController', ['$scope', function(scope) {
// Users for test
this.users = [
{
name: 'admin',
password: 'admin',
role: 'ADMIN'
},
{
name: 'employee',
password: '12345',
role: 'EMPLOYEE'
}
];
console.dir(this.users); // Prints an array of objects correctly
// called when user submits
scope.login = function() {
console.log('login(). User: ');
console.dir(scope.user); // Prints the object with user's input (also correct)
var found = false;
for(let u of this.users) {
console.log('Comparing with:');
console.dir(u);
if(u.name == scope.user.name && u.password == scope.user.password) {
console.log('Found!');
found = true;
// do something else...
}
}
if(!found) { // show error message... }
}
}]);
The problem is that when I submit the login form (call to scope.login()) I get an error message in the console:
TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
at m.scope.login (loginController.js:44)
...
loginController.js:44 corresponds to for(let u of this.users) { line. I searched through the web (W3 Schools, MDN and this site also) but the solutions didn't worked for me. I already tried the following solutions:
for(var u of this.users)var u; for(u in this.users)for(var i = 0; i < this.users.lenght; i++): This changes the error message toCannot read property 'length' of undefined
I'm feeling that it's something simple but I cant't figure out what it is (I'm not very skilled in Javascript, sorry). Can anyone help me with this issue?
Thanks in advance for your answers.
this.usersundefined?this? Should you not usescopeor just a normalvarorlet? Because when you get to the function,thisrefers to the functioncontroller assyntax. It is preferred in favor of$scope