Kafui Adjogatse
Posts: 0
Joined: Fri May 15, 2015 1:55 pm

Issues pushing an object to array of objects in localStorage

I am building an app relying solely on Model and Storage on the client. I am getting an error message stating that "push()" is not a function when I try to insert an object into an array of objects stored in localStorage. I have used the following JS as I have seen in other responses on the forum:

function newTab(item, varName) {
var array = JSON.parse(localStorage.getItem(varName));
if (array == null) {
array = new Array();
}
array.push(item);
localStorage.setItem(varName, JSON.stringify(array));
}

With:

var tab = {};
tabName = Apperyio("tabNameInput").val();
members = Apperyio.storage.AddMembers.get();

newTab(tab,'tabs');

sessionStorage.clear();

Also, I have a similar piece of code which appears to be working but is fundamentally the same. The main difference is that it is a string being pushed into an array, but the "push()" function is recognised:

function save(item, varName) {
var arr = JSON.parse(sessionStorage.getItem(varName));
if (arr == null) {
arr = new Array();
}
arr.push(item);
sessionStorage.setItem(varName, JSON.stringify(arr));
}

function myload(varName) {
var arr = JSON.parse(sessionStorage.getItem(varName));
if (arr == null) {
arr = new Array();
}
return arr;
}

With:

save(Apperyio('MemberInput').val(), 'AddMembers');
setSelectMenuOptions(Apperyio('memberList'), myload('AddMembers'));
Apperyio("MemberInput").val("");

Please can you check what I am doing incorrectly?

Image

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Issues pushing an object to array of objects in localStorage

Hello,

You can expand the error to see line with error.
Please check value of the "array" variable, it must be array before using push() method.

Kafui Adjogatse
Posts: 0
Joined: Fri May 15, 2015 1:55 pm

Issues pushing an object to array of objects in localStorage

Hi Sergiy,

The error points to line 35 in my JS:
array.push(item);

This is despite it working esrlier with arr.push(item). I've attached my Model and Storage pages as well Image Image

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Issues pushing an object to array of objects in localStorage

What value does variable "array" have before the line:
codevar array = JSON.parse(localStorage.getItem(varName)); /code?

Kafui Adjogatse
Posts: 0
Joined: Fri May 15, 2015 1:55 pm

Issues pushing an object to array of objects in localStorage

Hi Sergiy,

"array" is undefined prior to that line in a similar way as "arr" is undefined prior to the "save" and "myload" functions that appear to work.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Issues pushing an object to array of objects in localStorage

It have to be defined before pushing a new array item. You can use try/catch construnctions: https://developer.mozilla.org/en-US/d...

or use our implementation of the pushing item into array: https://getsatisfaction.com/apperyio/...

Kafui Adjogatse
Posts: 0
Joined: Fri May 15, 2015 1:55 pm

Issues pushing an object to array of objects in localStorage

Does this work with pushing into the predefined model or do I need to try an alternative method?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Issues pushing an object to array of objects in localStorage

It works with every storage variable with type=array. It can be array of numbers/boolean/strings/objects

Kafui Adjogatse
Posts: 0
Joined: Fri May 15, 2015 1:55 pm

Issues pushing an object to array of objects in localStorage

OK great, it seems to be performing better but I am still having some issues. I am effectively trying to push a name and member list into my predefined model. I am using the storagePush to do the name and then using mapping from sessionStorage for the member list. However, I am encountering two problems:
1) The form of each "tab" is not always as expected
2) Each new object is not being added to the array, instead it is overriding

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Issues pushing an object to array of objects in localStorage

Unfortunately it is not clear enough. Could you please provide more details, screen shots would help a lot.

Return to “Issues”