Paul Medawar
Posts: 0
Joined: Thu Apr 03, 2014 10:55 am

Server Code Sum - params.sort

Hi,

I'm succesfully using the following server code to sum the "Guests" column for each "Member" in a collection called "Visits".

----------------------------

var DB_id = "*********",
collectionName = "Visits",
columnNamePlayer1 = 'Member',
columnNameScore = 'Guests';

try {
var result = [];
query1 = Collection.distinct(DB_id, collectionName, columnNamePlayer1);

for (var j = 0; j < query1.length; j++) {
var params = {}; //Define parameters object

Code: Select all

 params.criteria = { //Query criteria:  
   'Member': query1[j] 
   }; 

 var query2 = Collection.query(DB_id, collectionName, params); 

 console.log(params); 

 var i, len, value; 
 var sum = 0; 
 for (i = 0, len = query2.length; i < len; i++) { 
   value = parseFloat(query2[i][columnNameScore]); 
   if (value) sum += value; 

 } 

 result.push({ 
   Member: query1[j], 
   totalGuests: sum, 

 }); 

}

response.success(result, "application/json");
} catch (e) {
response.success({
message: e.message,
code: e.code
}, "application/json");
}

-----------------------------------

I would like to use params.sort to sort the response so that it is in "Member" order. I'm trying iterations of the following with no success.

-----------------------------------

var DB_id = "*********",
collectionName = "Visits",
columnNamePlayer1 = 'Member',
columnNameScore = 'Guests';

try {
var result = [];
query1 = Collection.distinct(DB_id, collectionName, columnNamePlayer1);

for (var j = 0; j < query1.length; j++) {
var params = {}; //Define parameters object

Code: Select all

 params.criteria = { //Query criteria:  
   'Member': query1[j] 
   }; 
 params.sort = "Member"             < len; i++) { 
   value = parseFloat(query2[i][columnNameScore]); 
   if (value) sum += value; 

 } 

 result.push({ 
   Member: query1[j], 
   totalGuests: sum, 

 }); 

}

response.success(result, "application/json");
} catch (e) {
response.success({
message: e.message,
code: e.code
}, "application/json");
}

-----------------------------------

can you help with how to correctly use params.sort in this example please?

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

Server Code Sum - params.sort

Hi Paul and Happy Monday,

Since you're doing pretty well with the server script, I won't supply a super amount of detail - but I hope to lead you in the right direction.

A query 'executes' / retrieves data from the server against the database when you issue your 'command' to get the data (shown here)....

....
var result = [];
query1 = Collection.distinct(DB_id, collectionName, columnNamePlayer1);
...

If you want to SORT the data and / or limit the data - you must do it PRIOR to retrieving the data from the server (prior to asking for Collection.distinct). So - you create a params object like so: (like you have above)...

var params = {};
params.sort = "Member" // if you want the data to come back in member order.....
params.criteria = {} // Whatever you are going to make the criteria

and then execute the query....with the params and with the sort.....

query1 = Collection.distinct(DB_id, collectionName, columnNamePlayer1, params);

You can't specify these after the execution of the query (as your code above is doing) - it must be before.

Hope that helps....

Bruce

Image

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

Server Code Sum - params.sort

Hello,

We suggest you don't use queries in the loop because it might cause the timeout error.

If you need to calculate all Members of Visits, please add the pointer to the member in the Visits collection. It will help you to remove that loop and you will calculate sum without problems

Paul Medawar
Posts: 0
Joined: Thu Apr 03, 2014 10:55 am

Server Code Sum - params.sort

Hi both, thank you for the replies. This project is on hold for me at the moment, when i get back to it, i will look at your recommendations.

Thanks for your help

Return to “Issues”