简体   繁体   English

TypeError:无法使用AngularJS从数组读取未定义的属性“ 0”

[英]TypeError: Cannot read property '0' of undefined from the Array using AngularJS

I am trying to create a webpage where when a user logs in from the login page, my page that is the 'user management' page should display users belonging to the login user's company. 我正在尝试创建一个网页,当用户从登录页面登录时,我的页面即“用户管理”页面应显示属于登录用户公司的用户。

For eg: When a user belonging to company APPLE logs in, this webpage should only display users belonging to APPLE. 例如:属于APPLE公司的用户登录时,此网页应仅显示属于APPLE的用户。

In the code, "currentUser" is the login user I get from the login page via localStorage getItem. 在代码中,“ currentUser”是我通过localStorage getItem从登录页面获得的登录用户。 "allUsers" array is the list of all users from different companies. “ allUsers”数组是来自不同公司的所有用户的列表。 Empty array "user" should display only display Current user's company's-users from the list of allUsers. 空数组“用户”应仅显示allUsers列表中的当前用户公司的用户。

For that I have used FOR LOOP seen at the end of the JS file. 为此,我使用了在JS文件末尾看到的FOR LOOP。 I'm getting a error that the property 'company' is undefined. 我收到一个错误,指出“公司”属性未定义。 Looking for someone to help me. 寻找某人来帮助我。 Thanks in advance. 提前致谢。

  var myApp = angular.module("myApp", []);

    myApp.controller("myController", function ($scope) {
        console.log("in controller...");
        $scope.newUser = {};
        $scope.info = "";

        if ($scope.users = JSON.parse(localStorage.getItem("users")) !== null) {

            $scope.users = JSON.parse(localStorage.getItem("users"));
        }

        if (localStorage.getItem("currentUser") !== null) {

            var currentUser = JSON.parse(localStorage.getItem("currentUser"));
            console.log("Received");
        }
        else {
            console.log("Not received");
        }

        if (localStorage.getItem("allUsers") === null) {
            $scope.allUsers = [
                { email: "John@yahoo.com", password:"John123", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
                { email: "Rick@yahoo.com", password: "Rick123", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
                { email: "Sam@yahoo.com", password: "Sam123", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
            ];
            localStorage.setItem("allUsers", JSON.stringify($scope.allUsers));
            // localStorage.setItem("users", JSON.stringify($scope.users));
        } else {
            $scope.allUsers = JSON.parse(localStorage.getItem("allUsers"));
        }

        //filter allUsers based on currentUser's role
        for (var i = 0; $scope.allUsers.length; i++) {
            $scope.users = [{}];
            if ($scope.allUsers[i].company === currentUser[0].company) {
                $scope.users.push($scope.allUser[i]);
            }
            localStorage.setItem("users", JSON.stringify($scope.users));
        }
    });

localStorage.getItem() returns string representation of null, if the key has null value in localStorage so your null check might not be behaving like expected. 如果键在localStorage中具有null值,则localStorage.getItem()将返回null的字符串表示形式,因此您的null检查可能不符合预期。 I'd recommend to wrap getItem in a function that would check that. 我建议将getItem包装在检查该功能的函数中。

function(itemName) {
  const item = localStorage.getItem(itemName);
  return item === 'null'
    ? null
    : item;
}

Your problem is probably in the place where you set currentUser, maybe because of the same null check. 您的问题可能在您设置currentUser的地方,可能是由于相同的null检查。

Also try declaring your i variable in for loop. 也可以尝试在for循环中声明i变量。

for(var i = 0; ...) ...

Edit 编辑

There seems to be an error in this line 该行似乎有错误

if ($scope.allUsers[i].company === currentUser[0].company) {
  $scope.users.push($scope.allUser[i]);
}

You're trying to push $scope.allUser[I] which is not defined, you've probably meant to write $scope.allUsers[I]. 您正在尝试推送未定义的$ scope.allUser [I],您可能打算编写$ scope.allUsers [I]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TypeError:无法读取未定义的属性'名称'-使用angularjs - ERROR TypeError: Cannot read property 'name' of undefined - Using angularjs 使用 AngularJS 和 Ionic 出现错误“TypeError:无法读取未定义的属性‘email’” - Error "TypeError: Cannot read property 'email' of undefined" using AngularJS and Ionic 类型错误:无法使用 AngularJS 读取未定义的属性“推送” - TypeError: Cannot read property 'push' of undefined with AngularJS AngularJS:TypeError:无法读取未定义的属性“childNodes” - AngularJS : TypeError: Cannot read property 'childNodes' of undefined angularJS)TypeError:无法读取未定义的属性“ push” - angularJS) TypeError: Cannot read property 'push' of undefined TypeError:无法读取angularjs中未定义的属性“ reduce” - TypeError: Cannot read property 'reduce' of undefined in angularjs AngularJS - TypeError:无法读取未定义的属性'go' - AngularJS - TypeError: Cannot read property 'go' of undefined Angularjs / Ionic TypeError:无法读取未定义的属性'then' - Angularjs/Ionic TypeError: Cannot read property 'then' of undefined TypeError:无法读取AngularJS上未定义的属性“ get” - TypeError: Cannot read property 'get' of undefined on AngularJS AngularJS:TypeError:无法读取未定义的属性“ get”? - AngularJS: TypeError: Cannot read property 'get' of undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM