Page 1 of 3

"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Mon Feb 10, 2014 6:54 pm
by kabrams

Hello, I'm trying to set up input search criteria on 2 fields, then based on input values, search collection then perform average on each field. I have my javascript [to calculate average] working but I'm not sure what the code for the 'edit js' button on 'where' request should be.

This is my 'edit js' code on 'where' request but I don't think it's right. I have another js running on event for service which calculates the average.

console.log('{"incident_state":"' + Appery('report_input_state').val() + '", "incident_county":"'+Appery('report_input_county').val()+'"}');
return '{"incident_state":"' + Appery('report_input_state').val() + '", "incident_county":"'+Appery('report_input_county').val()+'"}';

Does that 'edit js' look right? it's mapping to a storage variable called 'where'


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Mon Feb 10, 2014 7:00 pm
by kabrams

The output should be an average for each of these variables

Image


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Mon Feb 10, 2014 7:08 pm
by kabrams

On success event, I'm running this js to get average as a letter grade.

var correspondence = "FDCBAA"; // last A is for a perfect 5
var courtesyVal = 0, overallVal = 0;

if (data.length) {
for (var i = 0; i < data.length; i++) {
courtesyVal += data.grade_courtesy;
overallVal += data.grade_overall;
}

courtesyVal = (courtesyVal / data.length);
overallVal = (overallVal / data.length);
}

courtesyVal = correspondence[Math.floor(courtesyVal)];
overallVal = correspondence[Math.floor(overallVal)];

Appery("courtesy_grade").text(courtesyVal);
Appery("overall_grade").text(overallVal);


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Mon Feb 10, 2014 7:47 pm
by Kateryna Grynko

Hi,

You should use query parameter code$and/code: http://docs.mongodb.org/manual/refere...


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Mon Feb 10, 2014 9:52 pm
by kabrams

seriously? use this where? in the editjs? in the calculation js? Where and how? Your response is cryptic. I've almost got this working. I just need a little help.


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Tue Feb 11, 2014 12:03 am
by kabrams

Hello, can you please give me a little more direction


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Tue Feb 11, 2014 4:47 am
by Illya Stepanov

Hello - here is shown how to use $and operator: http://docs.appery.io/documentation/b...

pre{"$and": [{"priority": "Low"},{"taskName": "Check email"}]}/pre


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Tue Feb 11, 2014 4:33 pm
by Kateryna Grynko

Hi,

Did you add JavaScript code on parameter 'where' mapping? If yes, there should be the following code:prereturn '{"$and": [{"incident_state":"' + Appery('report_input_state')&#46;val() + '"}, {"incident_county":"'+Appery('report_input_county')&#46;val()+'"}]}';/pre


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Tue Feb 11, 2014 7:42 pm
by kabrams

Thanks much, but that still doesn't work. I've got the return statement in my editjs 'where'. It's mapped to a local storage variable. My script above which calculates average runs on the success event of the query and nothing happens.

when i remove the 'where' js, it runs, but doesn't when I add code to the 'where' parameter. Any ideas?


"edit js' code on where for query which takes input values for 2 variables and does collection lookup, then allows anoth

Posted: Tue Feb 11, 2014 7:54 pm
by kabrams

This is the result i'm getting

Image