0

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);
});

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

so that code run it on background and then run the next code?
When the function getChat runs, it starts a Find of a set of objects, then it exits. The find is still running and response.success() has not yet been called, so the context is kept around. When the Find completes, the block is run. If the block then calls response.success(), the original call will be completed and the context deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.