Page 1 of 1

Issues pushing an object to array of objects in localStorage

Posted: Mon Nov 02, 2015 10:24 pm
by Kafui Adjogatse

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


Issues pushing an object to array of objects in localStorage

Posted: Tue Nov 03, 2015 5:38 pm
by Serhii Kulibaba

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.


Issues pushing an object to array of objects in localStorage

Posted: Tue Nov 03, 2015 5:47 pm
by Kafui Adjogatse

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


Issues pushing an object to array of objects in localStorage

Posted: Tue Nov 03, 2015 9:53 pm
by Serhii Kulibaba

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


Issues pushing an object to array of objects in localStorage

Posted: Wed Nov 04, 2015 10:00 am
by Kafui Adjogatse

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.


Issues pushing an object to array of objects in localStorage

Posted: Thu Nov 05, 2015 9:42 pm
by Serhii Kulibaba

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/...


Issues pushing an object to array of objects in localStorage

Posted: Thu Nov 12, 2015 10:40 pm
by Kafui Adjogatse

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


Issues pushing an object to array of objects in localStorage

Posted: Fri Nov 13, 2015 12:59 pm
by Serhii Kulibaba

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


Issues pushing an object to array of objects in localStorage

Posted: Sat Nov 28, 2015 6:48 pm
by Kafui Adjogatse

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


Issues pushing an object to array of objects in localStorage

Posted: Mon Nov 30, 2015 10:00 pm
by Serhii Kulibaba

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