Search code examples
angularjsjsonlocal-storageupdating

Unexpected token o in JSON at position 0 at JSON.parse using angular


I have looked at simular threads, but to no success. What I'm trying to do is update my localstorage through an update function. The functions look as follows:

The code to make the variables to call:

var localProfile = JSON.parse(localStorage.getItem("profile"));

if(localProfile != undefined && localProfile.length>0)
{ this.profiles = localProfile; }

else {
    this.profile = [
        { id: "1526", name: "berserk",      password: "berserk",    age: "31",          gender: "male"},
        { id: "1358", name: "Johnathan",    password: "test",       age: "17",          gender: "male"},
        { id: "2539", name: "Britney",      password: "test",       age: "18",          gender: "female"},
        { id: "1486", name: "Kevin",        password: "test",       age: "7",           gender: "male"},
        { id: "7777", name: "jesus",        password: "holy",       age: "unknown",     gender: "male"},
        { id: "6666", name: "satan",        password: "hell",       age: "unknown",     gender: "unknown"}
    ];
}

The code to update the variable:

this.updateProfile = function(profile) {
    profile.updating = false;
    console.log(profile);
    localStorage.setItem("profile", profile);
}

As I noted in the title I am currently using Angular. I have used the console.log(-line and the response seems to be exactly what it's supposed to be. I have tried using JSON.parse( and JSON.stringify as well as a couple of other combinations. I seem to get either the error above or another error when trying to reload the page. Apperently I either cannot execute the statement, or I end up corrupting the data so reloading the page returns a simular error.

In case the data in variable profile is in doubt:

Array [ Object, Object, Object, Object, Object, Object ]

And when taking a closer look at the data:

age:"17"
gender:"male"
id:"1358"
name:"johnathan"
password:"test"

The other object looks identical with no weird defaults in them. I already took care of the $$hashkey just incase that was the problem.

Any help on how to execute the call correctly is greatly apreciated and if the information is insufficient please do tell.


Solution

  • The problem is your not using JSON.stringify when saving your data. So when you are parsing from localStorage its not json.

    Add a factory to be used across your application that handles JSON.parse() and JSON.stringify()

    .factory('LocalStorageUtil', function() {
      return {
        get: get,
        set: set,
        remove: remove
      }
    
      function get(key) {
        return JSON.parse(localStorage.getItem(key));
      }
    
      function set(key, val) {
        localStorage.setItem(key, JSON.stringify(val));
      } 
    
      function remove(key) {
        return localStorage.removeItem(key);
      }
    })
    

    Here is a JS Bin tiny sample app using this LocalStorageUtil.

    http://jsbin.com/fijagiy/2/edit?js,output