Page 1 of 1

Can server code store variables ?

Posted: Mon Jun 30, 2014 12:27 pm
by Bad Addy

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 ?


Can server code store variables ?

Posted: Mon Jun 30, 2014 2:19 pm
by Serhii Kulibaba

Hello Addy,

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


Can server code store variables ?

Posted: Mon Jun 30, 2014 2:31 pm
by Bad Addy

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


Can server code store variables ?

Posted: Mon Jun 30, 2014 3:27 pm
by Bad Addy

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


Can server code store variables ?

Posted: Mon Jun 30, 2014 4:09 pm
by Serhii Kulibaba

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.


Can server code store variables ?

Posted: Mon Jun 30, 2014 6:59 pm
by Bad Addy

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.


Can server code store variables ?

Posted: Wed Jul 02, 2014 1:59 am
by Yurii Orishchuk

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.


Can server code store variables ?

Posted: Sun Feb 22, 2015 11:52 pm
by Bruce Stuart

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


Can server code store variables ?

Posted: Mon May 23, 2016 8:13 am
by Amr Osman

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