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.
In the code, "currentUser" is the login user I get from the login page via localStorage getItem. "allUsers" array is the list of all users from different companies. Empty array "user" should display only display Current user's company's-users from the list of allUsers.
For that I have used FOR LOOP seen at the end of the JS file. 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. I'd recommend to wrap getItem in a function that would check that.
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.
Also try declaring your i variable in for loop.
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].
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.