Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

Can server code store variables ?

If I needed to run a server code twice for it to action correctly, am I able to store any variables from the first pass, to use in the second pass ?

Like localStorage, has server code this functionality ?

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

Can server code store variables ?

Hello Addy,

Server code doesn't have localStorage variables. You can store it in database (http://devcenter.appery.io/documentat...)

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

Can server code store variables ?

Good idea, I did not think of that. Thank you

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

Can server code store variables ?

Although that is a good idea, I still need to store the _id somewhere, otherwise how will it know what object to pull ?

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

Can server code store variables ?

If it is just one _id, you can write it in server code directly. E.g. Write all information you need in one DB item and read it by its id.

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

Can server code store variables ?

I cannot do that, the information comes from two locations. Hence why I said I need to run the same script twice. Both parts are needed to action the script to the fullest. I have managed to work a way round it though.

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

Can server code store variables ?

Hi Addy.

1 You need to create "scriptsData"("parameterName"(string), "parameterValue"(string)) collection in your DB.

2 Add row to this collection with name "firstScriptItemId" and empty value. http://prntscr.com/3yimzz/direct

3 Add this JS code to your script:

pre

//Set this variables with your values.
var dbID = "52fd3d06e4b0a25c11c89917";
var collectionName = "scriptsData";

var DBLSV = {

getItemId: function(itemName){

Code: Select all

 var params = {}; 
 params.criteria = { 
   "parameterName": itemName 
 }; 

var result = Collection.query(dbID, collectionName, params);

Code: Select all

 if(result[0]) 
   return result[0]._id; 

 return 0; 

},

getItem: function(itemName){

Code: Select all

 var params = {}; 
 params.criteria = { 
   "parameterName": itemName 
 }; 

var result = Collection.query(dbID, collectionName, params);

Code: Select all

 if(result[0]) 
   return result[0].parameterValue; 

 return ""; 

},

setItem: function(itemName, itemValue){
var itemId = this.getItemId(itemName);

Code: Select all

 var result = Collection.updateObject(dbID, collectionName, itemId, {"parameterValue": itemValue}); 

}
}

/pre

4 Now you can use the following code to get and set "firstScriptItemId" like LSV.

pre

//Set item.
DBLSV.setItem("firstScriptItemId", "helloWorld");

//Get item.
var item = DBLSV.getItem("firstScriptItemId");

/pre

Regards.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Can server code store variables ?

FYI - it's great that this was here - I was able to quickly adapt the "Collection.updateObject" code - and put it into a process I just wrote...

Thanks Yurii....

Amr Osman
Posts: 0
Joined: Sat Nov 14, 2015 11:58 am

Can server code store variables ?

Yurii , Thumps UP, I find allmost any problem I face using your answers.

Return to “Issues”