I've just started working in Parse by using the cloud code guide https://parse.com/docs/cloud_code_guide but stuck in averageStars project. This is my code:
var Review = {
"movie" : "The Matrix",
"stars" : 5
};
Parse.Cloud.define("averageStars", function(request, response) {
var query = new Parse.Query("Review");
query.equalTo("movie", request.params.movie);
query.find({
success: function(results) {
var sum = 0;
for (var i = 0; i < results.length; ++i) {
sum += results[i].get("stars");
}
response.success(sum / results.length);
},
error: function() {
response.error("movie lookup failed");
}
});
});
However, after I run the code by using command in the guide, it just returned blank {}.
This is my call to cloud code:
Maou-MacBook-Pro:MyCloudCode maou$ parse deploy
Uploading source files
Finished uploading files
New release is named v22 (using Parse JavaScript SDK v1.3.4)
Maou-MacBook-Pro:MyCloudCode maou$ curl -X POST \
> -H "X-Parse-Application-Id: tnXgGeoY38YjEbAqcakncPFRkUzaBQ29v5tK1ir7" \
> -H "X-Parse-REST-API-Key: 1YVzcl0aKATUUHZITsPasP11m0VNLrdVWiisE1YB" \
> -H "Content-Type: application/json" \
> -d '{"movie":"The Matrix"}' \
> https://api.parse.com/1/functions/averageStars
{}
++i. Is that a typo? I always prefer to use Crockford's advice and usefor (var i = 0; i < whatever; i = i + 1). Very obvious what it's doing. - mbm29414console.log()statements. You might not be getting aresultsarray that you expect. Try this (just beforevar sum = 0;):console.log("results: " + JSON.stringify(results));. - mbm29414