Randy7611273
Posts: 0
Joined: Sat Jun 27, 2015 7:03 pm

How to update Local Storage Array

Hello!

I have created a Generic Service which properly reads and filters a Local Storage Array. I cannot, however, seem to Update an item in said Array.

I would like for the user to click a button and change a Boolean value in the Array from “false” to “true.” I’ve tried dozens of methods, from modifying the JS code in the Generic Service to adding JS in the Service Request Mapping, but have not yet been able to make it work.

The item in the Array needing to be updated is votedReturn where it equals variable clickedSong. Again, the cdata.filter works beautifully: I just can’t update the related item.

Any assistance is greatly appreciated!

Thanks,
Randy

Randy7611273
Posts: 0
Joined: Sat Jun 27, 2015 7:03 pm

How to update Local Storage Array

Appery.localService2JS = Appery.createClass(null, {
init : function(requestOptions) {
this.requestOptions = $.extend({}, requestOptions);
},
process : function(settings) {
if (this.requestOptions.echo) {
settings.success(this.__requestOptions.echo);
} else {
var cdata = JSON.parse(localStorage.getItem("liveEventArray"));
var thisVote = (localStorage.getItem("clickedSong"));
var searched = thisVote;

Code: Select all

         [b]var voteReturn = (localStorage.setItem('votedReturn', ({ 
             thisVote: true})));[/b] 

         var match = cdata.filter(function(item) {  
             return item._id === searched;  
         });  
         settings.success(match);  
         settings.complete('success'); 
     } 
 }  

});

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

How to update Local Storage Array

Hello Randy,

It is better to use storage variable instead of simple localStorage:
https://devcenter.appery.io/documenta...

it has update method

Randy7611273
Posts: 0
Joined: Sat Jun 27, 2015 7:03 pm

How to update Local Storage Array

I've reviewed the link, along with countless other posts... In my Generic Service I am trying several variations of the following, all of which are throwing errors:

Code: Select all

         var match = cdata.filter(function(item) {  
         [b] votedReturn.update({"$['thisVote']": true}); [/b] 
         return item._id === searched;  
          });  

I don't know either the proper syntax, or the location, for the needed code. To clarify: I need to change votedReturn to true where it equals item._id, which does return properly.

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

How to update Local Storage Array

You have to use JS like this:
preApperyio.storage.votedReturn.update("$['thisVote']", true);/pre

for setting storage variable's (votedReturn.thisVote) value = true

Randy7611273
Posts: 0
Joined: Sat Jun 27, 2015 7:03 pm

How to update Local Storage Array

Using that line of code returns the following error: "TypeError: Cannot read property 'update' of undefined at Appery.localService2JS.Appery.createClass.process"

Below is my complete Generic Service:

Appery.localService2JS = Appery.createClass(null, {
init : function(requestOptions) {
this.requestOptions = $.extend({}, requestOptions);
},
process : function(settings) {
if (this.requestOptions.echo) {
settings.success(this.__requestOptions.echo);
} else {
var cdata = JSON.parse(localStorage.getItem("liveEventArray"));
var thisVote = (localStorage.getItem("clickedSong"));
var searched = thisVote;

Code: Select all

         [b] Apperyio.storage.votedReturn.update("$['thisVote']", true);[/b] 

         var match = cdata.filter(function(item) {  
         return item._id === searched;                        
         });  

         settings.success(match);  
         settings.complete('success'); 
     } 
 }  

});

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

How to update Local Storage Array

Hi Randy,

If following code:

pre

Apperyio.storage.votedReturn.update("$['thisVote']", true);

/pre

"votedReturn" is your storage name. If you have another storage name you should use your one.

Also "thisVote" is your storage attribute that you want to update.

If you will still have this problem please show us:

1 Your model("Storage and model" interface).
2 Your storage("Storage and model" interface).
3 Current storage value(you can use js code to get it in console):

pre

console.log(Apperyio.storage.votedReturn.get());

/pre

Regards.

Randy7611273
Posts: 0
Joined: Sat Jun 27, 2015 7:03 pm

How to update Local Storage Array

Hello Yurii,

I apologize if I am not using the proper terminology or syntax. Below are screenshots of my Model and Storage.

Using this code console.log(Apperyio.storage.votedReturn.get()); throws errors: TypeError: Cannot read property 'get' of undefined at Appery.localService2JS.Appery.createClass.process

I assume the errors are because votedReturn is contained in the Array. Using this code console.log(Apperyio.storage.clickedSong.get()); returns a proper, expected value.

To clarify: I am trying to set votedReturn to TRUE in Local Storage Array (liveEventArray) where _id = clickedSong.

I believe I’ve set up my Local Storage Array (liveEventArray) properly, as my filtering does work with queries: I just can’t get it to update.

Thanks!
Randy

Return to “Issues”