The following Cloud Code function on Parse Server does not work as expected.
It seems to resolve too early so that not all chained promises of promise2
are executed. In particular I see some of the resultA.add(...);
not being executed.
I have tried to re-arrange this several times without success.
I am not sure if return Parse.Promise.resolve();
is what I should return in the else
clause or if it is something else or nothing.
Can anyone spot something here?
Parse.Cloud.define("aFunction", function(request, response) {
var q = new Parse.Query("ClassA");
q.find()
.then(
function(resultsA) {
var promise = new Parse.Promise.as();
resultsA.forEach(function(resultA) {
promise = promise
.then(
function() {
var q = new Parse.Query("ClassB");
return q.first();
}
)
.then(
function(resultB) {
if (resultB != undefined) {
resultA.set(...);
return resultA.save();
} else {
resultA.set(...);
return resultA.save();
}
}
)
.then(
function() {
var q = new Parse.Query("ClassC");
return q.find()
.then(
function(resultsC) {
if (resultsC != undefined && resultsC.length > 0) {
var promise2 = new Parse.Promise.as();
resultsC.forEach(function(resultC) {
promise2 = promise2
.then(
function() {
resultA.add(...);
return resultA.save();
}
);
});
return promise2;
} else {
return Parse.Promise.resolve();
}
}
);
}
);
});
return promise;
}
)
.then(
function(result) {
response.success("success");
},
function(error) {
response.error(error.message);
}
);
});
Long code but I already stripped it down and wanted to show the actual structure. Thanks.