Reputation: 429
i have defined a parse cloud code call getChat (as below), but when i run it, it doesn't return any results
Parse.Cloud.define("getChat", function(request, response) {
var allchat = [];
var query = new Parse.Query("chat");
query.find().then(function(results) {
console.error("test"); //nothing in console
console.error(results.length); //nothing in console
for (var i = 0; i < results.length; ++i) {
for(var iii = 0; iii<results[i].get("limitleft").length; iii+=2){
if(results[i].get("limitleft")[iii] == request.params.user){
allchat.push(results[i]);
}
}
}
});
response.success(allchat);
});
Upvotes: 0
Views: 941
Reputation: 1145
Query.find() returns a Promise. ".then" attaches a callback function to that promise. When the Find completes, it executes the callback. However, you called response.success() right after you initiated the Find. The results have not been delivered yet. Calling response.success() effectively cancels the find because getChat has been completed by calling response.success().
Put the call to response.success() inside the block!
-Bob
Upvotes: 1