I'm using the following Snippet to retrieve an object previously stored in localStorage
but for some reason when trying to modify it the object remains unmutable.
This is how I create the object:
if(typeof(Storage) !== "undefined") {
if(!localStorage.Obj){
localStorage.Obj = new Object();
}
var Obj = localStorage.Obj;
} else {
if(!$.cookie("Obj")){
$.cookie("Obj", new Object(), {
expires : 7, // Days
domain : 'localhost:8080',
secure : true
});
}
var Obj = $.cookie("Obj");
}
This is where I try to modify the object:
var login = function(email, passwd){
$.ajax({
url: "http://127.0.0.1/api/v1/login",
type: "POST",
data: { email: email, password: SHA256_hash(passwd), platform: getBrowserName(), osVersion: getOsName()},
beforeSend: function(xhr){xhr.setRequestHeader('apiKey', "46d1e2f6-cf37-4275-bc1c-d674b12cc9bf");},
success: function(data, status, xhr){
Obj["logged_in"] = true;
Obj["sessionKey"] = data.sessionKey;
Obj["user_id"] = data.userID;
Obj["time_format"] = data.timeFormat;
Obj["first_name"] = data.firstName;
Obj["last_name"] = data.lastName;
},
error: function(data, status, xhr){
console.log(data);
}
});
if(Obj.logged_in){
window.location.replace("http://127.0.0.1/users/" + Obj.user_id + "/todos");
}
};
I need to point out 3 things:
- I'm using jQuery
- The 2 codes are in different files but the loading order is the correct.
- When using
console.log(Obj)
I get the following output:"[object Object]"
Thanks in advance.