Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Created January 18, 2016 17:00
Show Gist options
  • Save sjehutch/2f23ff8c5c3660f51dc7 to your computer and use it in GitHub Desktop.
Save sjehutch/2f23ff8c5c3660f51dc7 to your computer and use it in GitHub Desktop.
Parse cloud code get average of SUM Rating
Parse.Cloud.define("getrating", function(request, response) {
//Query class appointments
var query = new Parse.Query("appointments");
//Query column trainer in appointments pass trainerid object
query.equalTo("trainer", request.params.trainerid);
query.find({
success: function(results) {
var sum = 0;
for (var i = 0; i < results.length; ++i) {
//Get the sum of the field rate for the trainer
sum += results[i].get("rate");
}
response.success(sum / results.length);
},
error: function() {
response.error("Calculating ratings failed");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment