eric_nnv
Posts: 0
Joined: Tue Sep 16, 2014 3:40 am

Server Code Query By Username & Date

Trying to use a server code script to add up a column (counts) for the current user for the last 7 days as well as the last 30 days. Have made a number of attempts (reviewing documentation and other community threads) with no luck so I am pasting my original script which successfully sums up all steps for the current user.

Thanks in advance for any suggestions.

var DB_id='xxxx',
collectionName='Counts',
columnNameCounts = 'counts',
username = request.get("username");

var params = {}; //Define parameters object
params.criteria = { 'username': username //Query criteria:
}

try {
query = Collection.query(DB_id, collectionName, params);
var i, len, value, sum = 0;
for(i = 0, len = query.length; i < len; i++) {
value = parseFloat(query[columnNameCounts]);

if (value) {
sum += value;
}
}
response.success({totalSum: sum}, "application/json");
} catch (e) {
response.success({message: e.message, code: e.code}, "application/json");
}

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

Server Code Query By Username & Date

Hi Eric,

Your code looks ok,

Do you have any errors in traceinfo?

Please provide us screen shot with result of running this server code(including trace info panel expanded)

Regards.

eric_nnv
Posts: 0
Joined: Tue Sep 16, 2014 3:40 am

Server Code Query By Username & Date

Thanks for the quick response Yurii. My question is this: the collection also has a column called "date". What is the code I need to add to the existing script so it will sum the "counts" column for the logged in user for the last 7 days and the last 30 days. I have tried numerous things but have been unsuccessful.

As an FYI I will then take the result and pass it back through a service I have created and display it for the logged in user ( this I know how to do).

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

Server Code Query By Username & Date

Hi Eric,

Here is a correct and tested code for you:

pre

var userName = "userName";&#47;&#47;request&#46;get("userName"); &#47;&#47;Store the user name
var userPass = "123";&#47;&#47;request&#46;get("userPass"); &#47;&#47;and its password

var dbId = "52fd3d06333a25c11c89917";&#47;&#47; commented out
&#47;&#47;You should use here your collection name&#46;
var collectionName = "dogs";
var query = [] ;

&#47;&#47;You can adjust days here&#46;
var dayBefore = 7;
var untilDate = new Date(new Date()&#46;getTime() - (dayBefore * 24 * 3600 * 1000) );

console&#46;log("date = " + untilDate);

var untilDateFormated = untilDate&#46;toISOString()&#46;replace(/T/gi, " ")&#46;replace(/Z/gi, "");

try
{
result = {};

var token = DatabaseUser&#46;login(dbId, userName, userPass)&#46;sessionToken;

var params = {};

params&#46;criteria =
{
$and: [
&#47;&#47;First criteria, you need to replace it with yours&#46;
{name: "lala"},
&#47;&#47;Second criteria by date&#46;
{date: {$gt: untilDateFormated}}
]
};

query = Collection&#46;query(dbId,collectionName,params,token) ;

response&#46;success(query);
}
catch (e)
{
response&#46;success("message: " + e&#46;message + "ncode: " + e&#46;code);
}

/pre

Note: you should replace DBID, username, and password with yours, and replace second search criteria with yours.

Regards.

eric_nnv
Posts: 0
Joined: Tue Sep 16, 2014 3:40 am

Server Code Query By Username & Date

Thanks for your help Yurii!

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

Server Code Query By Username & Date

Hi There,
I am trying to understand something about server code :-)
I have got a look at the very top post calculating the sum of records.

in my case, I need to perform 2 queries one into the other :
. a query1.distinct to get player names
. a query2 associated to each player name[ j ] which sum up the games' results [ i ]

I tried the following code. it doesn't return any error msg :-)
however, I don't get any result either = the trace info window is simply blank :-(

I'm foreseeing (at least, you may find much more) :
a/ how to define the query2[ j ] of query1' name[ j ] ?
b/ the response syntax ?
c/ anything else ?

thk you for your time always,
Eric

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

var DB_id="xxx",
collectionName="2PlayGameResults",
columnNamePlayer1 = 'Player1',
columnNameScore = 'Score1';

try {

query1={};
query1.distinct = Collection.distinct(DB_id, collectionName, 'Player1');

for (var j = 0; j < query1.length; j++) {

Code: Select all

 var params = {}; //Define parameters object  

params.criteria = { //Query criteria:
'Player1': '"'+query1[j][columNamePlayer1]+'"'
};

Code: Select all

query2[j] = Collection.query(DB_id, collectionName, params);  

var i, len, value; 
 var sum = new Array();  

for(i = 0, len = query2.length; i < len; i++) {  

value = parseFloat(query2[columnNameScore]);

Code: Select all

if (value) {  

sum[j] += value;
}

}

response.success({Player: query1[j][columNamePlayer1], totalSum: sum[j]}, "application/json");
}
} catch (e) {
response.success({message: e.message, code: e.code}, "application/json");
}

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

Server Code Query By Username & Date

Hi EJLD,

There is a lot of errors in your code.

I've fixed them and here is a result:

precode

var DB_id = "52fdxxxxx1c89917",
collectionName = "dogs",
columnNamePlayer1 = 'name',
columnNameScore = 'number';

try {
var result = [];

query1 = Collection&#46;distinct(DB_id, collectionName, columnNamePlayer1);

for (var j = 0; j < query1&#46;length; j++) {

Code: Select all

 var params = {}; &#47;&#47;Define parameters object  
 params&#46;criteria = { &#47;&#47;Query criteria:  
   'name': query1[j] 
 }; 

 var query2 = Collection&#46;query(DB_id, collectionName, params); 

 console&#46;log(params); 

 var i, len, value; 
 var sum = 0; 

 for (i = 0, len = query2&#46;length; i < len; i++) { 

   value = parseFloat(query2[i][columnNameScore]); 

   if (value) sum += value; 
 }; 

 result&#46;push({ 
   Player: query1[j], 
   totalSum: sum 
 }); 

}

response&#46;success(result, "application/json");

} catch (e) {
response&#46;success({
message: e&#46;message,
code: e&#46;code
}, "application/json");
}

/code/pre

Please learn it to understand how it works.

Regards.

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

Server Code Query By Username & Date

Hi Yurii, thks for your help.

Just to let you know that it returns well "name":"lambda","sum":0
but "sum" always = 0 (zero) whoever name, even though it should be 0.
I'll try to fix it.

however, if you hv time, you are welcome to provide me with the working version.
thk you very much,

Eric

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

Server Code Query By Username & Date

Hi EJLD,

Sorry for delay.

Did you able to fix it?

Regards.

EJLD
Posts: 0
Joined: Sun Jun 08, 2014 11:03 pm

Server Code Query By Username & Date

Hi Yurii,

thks for your note.
no, in fact, I have not got time to look at it.
I worked on other things.

You would be very kind if you cld come up with the working solution.
This server code thing scares me :)

Promise, after that I'll learn by myself :)

thks for your time,
Best,
Eric

Return to “Issues”