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> 

3
  • There's nothing in this code to indicate which visitor's signout you want to update. Commented Apr 8, 2015 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 Commented Apr 9, 2015 at 7:37
  • I'll reply as an answer now that I know what you're trying to do. Commented Apr 9, 2015 at 16:54

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

4 Comments

So how do you propose i do it?
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)
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
how do i define the targetSignoutId variable?
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.

4 Comments

it is not updating the localstorage at all
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)
I can't seem to get it to work. do you think there is a problem in the HTML?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.