I am trying to store localStorage value in array and following this page Push JSON Objects to array in localStorage. My code is:
function SaveDataToLocalStorage(data)
{
var a = [];
// Parse the serialized data back into an aray of objects
a = JSON.parse(localStorage.getItem('session'));
// Push the new data (whether it be an object or anything else) onto the array
a.push(data);
// Alert the array value
alert(a); // Should be something like [Object array]
// Re-serialize the array back into a string and store it in localStorage
localStorage.setItem('session', JSON.stringify(a));
}
where data
is:
var data = {name: "abc", place: "xyz"}
I am getting the following error:
Uncaught TypeError: Cannot call method 'push' of null
Can anybody show the correct method to store localStorage values in array?
I am trying to store localStorage value in array and following this page Push JSON Objects to array in localStorage. My code is:
function SaveDataToLocalStorage(data)
{
var a = [];
// Parse the serialized data back into an aray of objects
a = JSON.parse(localStorage.getItem('session'));
// Push the new data (whether it be an object or anything else) onto the array
a.push(data);
// Alert the array value
alert(a); // Should be something like [Object array]
// Re-serialize the array back into a string and store it in localStorage
localStorage.setItem('session', JSON.stringify(a));
}
where data
is:
var data = {name: "abc", place: "xyz"}
I am getting the following error:
Uncaught TypeError: Cannot call method 'push' of null
Can anybody show the correct method to store localStorage values in array?
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Jan 5, 2014 at 17:07 user2567857user2567857 4838 silver badges25 bronze badges 1- 2 The variable "a" must be null. Use the debugger in your browser and check the value before the push call is run. I think that should put you on the right track – drew_w Commented Jan 5, 2014 at 17:10
2 Answers
Reset to default 7null is a special value for objects that aren't initialized to anything. My guess is that localStorage.getItem('session') is empty.
a more robust answer would be something like
function SaveDataToLocalStorage(data)
{
var a;
//is anything in localstorage?
if (localStorage.getItem('session') === null) {
a = [];
} else {
// Parse the serialized data back into an array of objects
a = JSON.parse(localStorage.getItem('session'));
}
// Push the new data (whether it be an object or anything else) onto the array
a.push(data);
// Alert the array value
alert(a); // Should be something like [Object array]
// Re-serialize the array back into a string and store it in localStorage
localStorage.setItem('session', JSON.stringify(a));
}
You're overriding the initial empty array that initializes "a" when you fetch the local storage contents. The variable is declared and initialized:
var a = [];
and then that empty array is immediately thrown away:
a = JSON.parse(localStorage.getItem('session'));
After that, it appears that your retrieved value is actually empty (null
) if you're getting that error.
If you want "a" to be either a new empty array, or else an array saved in local storage, you'd do something like this:
var a = localStorage.getItem('session') || "[]";
a = JSON.parse(a);