Reputation: 101
I'm a nascent coder that's new to async issues. For simplicity's sake, I've created a quick example to illustrate my problem. I have a simple function that runs a query and with the result, calls 2 other cloud functions. It then attempts to .set values for the returned object and .save() it. Everything seems to execute correctly, except the .save(). I've tried using promises/.thens, and a few other tricks, but nothing has worked. If someone could provide the most simple and direct solution, I would really appreciate it.
Parse.Cloud.define("test", function(request,response){
query.equalTo("name",request.params.name);
query.first(
success: function(result){
result.set("testAverage", Parse.Cloud.run("calcAverage",{"name":request.params.name,"type":"test"}));
result.set("quizAverage", Parse.Cloud.run("calcAverage",{"name":request.params.name,"type":"quiz"}));
result.save();
},
error: function(){
response.error("error");
}
);
return result;
});
P.S. in my actual scenario, there are between 10-20 parallel calls to other cloud functions, not just 2.
Thanks!
Upvotes: 1
Views: 4516
Reputation: 21
I believe with the way you are currently coding things that the result.save() may be called before your cloud functions are able to return. I'm also not sure that request.params.name will work properly when referenced inside of your success callback functions.
Make sure your version of parse supports promises, and then try organizing your code like this:
function queryTestAverage(name) {
return Parse.Cloud.run("calcAverage",{"name":request.params.name,"type":"test"});
}
Parse.cloud.define("test", function(request, response) {
query.equalTo("name", request.params.name);
return query.first()
})
.then(function(results) {
var name = results.get("name");
Parse.Promise.when([queryTestAverage(name), queryQuizAverage(name)]).then(
function(result1, result2) {
results.set("testAverage", result1);
results.set("quizAverage", result2);
results.save();
}
}
Note: Above code is for sample and may not work as is.
Sources of information:
Upvotes: 2