0

I am trying to create a user database using localStorage. I just need to store the created user with a unique id so I can reference it as a key later. Right now I have this code in my application.js:

$(document).ready(function() {
    $("#signupform").submit(function(e) {
        e.preventDefault();
        var user = {
            name: $('#pname').val(),
            email: $('#email').val()
        };
        localStorage.setItem('user', JSON.stringify(user));
        console.log(JSON.parse(localStorage.getItem('user')).name);
        var html = new EJS({url: 'templates/usershow.ejs'}).render(user);
        var content = document.getElementById('content');
        content.innerHTML = content.innerHTML + html;
        $("#signupform").remove();   
    });
});

I need localStorage.setItem('user'... to be something like localStorage.setItem('i'... where "i" increments with each newly created user (like an index) when the submit button is clicked. Would it make more sense to use the user email as the key? That way I can look up each user by their email? How would I go about doing this? Again...I am just trying to set up a user database where I can easily reference users and their info using localStorage. Show me how!

ewizard
  • 2,801
  • 4
  • 52
  • 110

2 Answers2

1

I'd go about incrementing an ID by storing a nextUniqueId in localStorage, then grab that for the key and increment it. Something like this:

function getNextUnique () {
    var next = localStorage.getItem('nextUniqueId');
    next = next ? parseInt(next) : 0;
    var newNext = next + 1;
    localStorage.setItem('nextUniqueId', newNext);
    return next;
}

Call it whenever you want to save a new user:

localStorage.setItem(getNextUnique(), JSON.stringify(user));
Will
  • 151
  • 9
  • cool...two questions...(i'll mark it as a correct answer as soon as I understand what is going on). I'm guessing this line `next = next ? parseInt(next) : 0;` checks for uniqueness...I couldn't find an explanation of what the "?" operator is. Also, where is `nextUniqueId` declared? Should it be a global variable? – ewizard Jun 03 '13 at 14:10
  • That line is a ternary operator, which is just a shorthand for an if/else statement. Basically, if there isn't a 'nextUniqueId' key in your localstorage, it will set the value to 0 (first time, fresh localStorage). If the key _is_ there, the value that comes out of local storage is a string, and we want it as an integer, so I wrap it in `parseInt()` -- there's no checking for uniqueness other than incrementing the value of `nextUniqueId` every time we ask for one. There isn't a `nextUniqueId` variable, just a key for it in your localStorage. – Will Jun 03 '13 at 15:25
  • cool thanks...it turns out i am looking for something much more complicated, i think (according to the below post) - i think i need a mysql database and myphpadmin - http://myphpadmin.net – ewizard Jun 03 '13 at 16:04
  • Take a look at something like [Parse](http://www.parse.com)... they can make something like storing user accounts and data really easy, with a syntax that's not far from using localStorage. You can go a long way on their free tier, too. – Will Jun 03 '13 at 17:19
1

It makes more sense to use the user's email. Simplicity is good.

BUT... this is localStorage. There shouldn't ever be more than one user using it, right? localStorage itself is unique to the client application. Are there really going to be multiple users using the same browser?

If so, then this is fine. But i wonder if you are really thinking about how localStorage works...

Nathan Bubna
  • 6,823
  • 2
  • 35
  • 36
  • sorry nathan...ur explanation went over my head a little...how do I keep track of user information than? are u suggesting implementing sql or something? – ewizard Jun 03 '13 at 14:41
  • i just found this...http://php-mysql.yoexpert.com/php-mysql-general/how-do-i-set-up-a-php-user-database-for-my-website-36864.html kinda looks like what i need - guess i need to learn some php – ewizard Jun 03 '13 at 15:05
  • 1
    Sharing a computer happens.. But still +1 for pointing out localStorage is indeed local (and for using email) - which means the data stored of the user will only be available on that browser/computer unless the data is synchronized to a server. –  Jun 03 '13 at 21:30
  • @ewizard. Yes it seems like you need a database not just localstorage – Michal Jun 04 '13 at 01:16