0

So i've got this piece of code to update the date and time a person leaves in localstorage

$scope.visitorout = function(){
var dateout1 = new Date(); 
var signout = JSON.parse(localStorage["allVisitors"]);
for(var i=0;i<signout.length;i++)
if (signout[i].id===signout[i].id) signout[i].dateout = dateout1;
localStorage["allVisitors"] = JSON.stringify(signout);
}; 

but whenever i call the function, it changes all the values of dateout for every single thing in local storage but i only want it to change just one

I have modified the code to this:

$scope.visitorOut = function(id){
var allVisitors = JSON.parse(localStorage["allVisitors"]);
var visitor;
for(var i=0; i<allVisitors.length; i++){
visitor = allVisitors[i];
if (allVisitors[i].id === visitor.id) {
visitor.dateout = new Date();
console.log(visitor.id)
break;
}
}
localStorage["allVisitors"] = JSON.stringify(allVisitors);

};

It updates the 'dateout' but for just the same item in localstorage and the console.log displays the same id each time...

<div class="list">
  <a class="item item-icon-left item-icon-right" ng-repeat="visit in visits | orderBy: '-date'" ng-click="visitorOut(id); closesignout()" ng-hide="visit.hide">
<i class="icon ion-person"></i>
{{visit.fname}} {{visit.lname}}
<i class="icon ion-log-out" ng-click="hideMe(visit)"></i>

  • There's nothing in this code to indicate which visitor's `signout` you want to update. – HankScorpio Apr 08 '15 at 22:59
  • Ok, I see what you mean. each visitor has a unique ID which is a random number generated by 'Math.random' if I wanted to record the date a visitor leaves when they click on their name, how would i modify the code? thanks – user3419780 Apr 09 '15 at 07:37
  • I'll reply as an answer now that I know what you're trying to do. – HankScorpio Apr 09 '15 at 16:54

2 Answers2

3

This code looks like a bug. You are comparing an object to itself (which will always be true):

if (signout[i].id===signout[i].id)
Travis Collins
  • 3,982
  • 3
  • 31
  • 44
  • So how do you propose i do it? – user3419780 Apr 09 '15 at 14:53
  • It looks like you're trying to find a particular element in the signout array. Which element are you trying to find? is there a variable somewhere that represents the item you're trying to find? Assuming that variable is called targetSignoutId, change the code to if(signout[i].id === targetSignoutId) – Travis Collins Apr 09 '15 at 15:06
  • yes i am trying to find the id of each visitor which is generated randomly once a user signs in. how do i find that id once the visitor clicks on their name on the signout page? the signout page displays the name of each visitor with and ng-click calling the 'visitorout' function. Thanks – user3419780 Apr 13 '15 at 10:26
  • how do i define the targetSignoutId variable? – user3419780 Apr 16 '15 at 22:29
0

This function needs to know which visitor to update. Wherever you're calling this function, you need to pass in at least the userId (if not the full user object) something like: $scope.visitorOut(user.id) or from html: <myElement ng-click="visitorOut(user.id)" />

$scope.visitorOut = function(userId){
    var allVisitors = JSON.parse(localStorage["allVisitors"]);
    var visitor;
    for(var i=0; i<allVisitors.length; i++){
        visitor = allVisitors[i];
        if (visitor.id === userId) {
            visitor.dateout = Date.now();
            break;
        }
    }
    localStorage["allVisitors"] = JSON.stringify(signout);
};

Note that I've used Date.now() here instead of new Date(). It's much easier to work with timestamps than Dates if you're going to stringify it. A timestamp can be converted to a Date like this:

var myDate = new Date(visitor.dateOut);

But if you want to convert a stringified date back to a Date object... it gets complicated (click).


I also changed the variable names a bit. Camelcase is a useful naming convention that aides readability, and from reading the rest of the code the signout variable seems to actually be an array of visitor objects, not an array of signouts, so I renamed that too.

Community
  • 1
  • 1
HankScorpio
  • 3,612
  • 15
  • 27
  • it is not updating the localstorage at all – user3419780 Apr 14 '15 at 23:39
  • You made the same mistake again of comparing each visitor to itself. Change this: `if (allVisitors[i].id === visitor.id)` to this: `if (visitor.id === id)` – HankScorpio Apr 15 '15 at 02:00
  • I can't seem to get it to work. do you think there is a problem in the HTML? – user3419780 Apr 15 '15 at 21:40
  • Yes, there is. `visitorOut(id)` should be `visitorOut(visitor.id)`. Take a look through all your code and html, look at every variable, write it down and make sure you know where each one should be coming from. Go slow with it, write them down if it helps. I strongly recommend that you learn how to use a debugger. If you were to set a breakpoint anywhere in that function you'd see that `id` is undefined. – HankScorpio Apr 15 '15 at 22:21