解析云代码承诺未正确返回

时间:2017-06-16 12:02:06

标签: javascript parse-platform promise cloud-code

我以为我已经弄清楚了这个Promise的事情,但是这个函数从Query查询所有结果而不管1000项限制是不是正常工作。谁能向我解释我在哪里出错了?谢谢!

此Cloud Job位于我的main.js文件中:

<div>{{getId()}}</div>

此函数位于queryHelper.js:

//in main.js
Parse.Cloud.job("testQuery", function(request, status) {
  var theClass = Parse.Object.extend("SomeClass");
  var theQuery = new Parse.Query(theClass);
  console.log("Here we go...");
  queryHelper.getAllResultsForQuery(theQuery, console).then( function(result) {
    //This code is called before getAllResultsForQuery finishes
    console.log("Finished the search!");
    status.success("yay");
  }, function(error) {
    console.log("Fail " + error);
    status.error("failed!");
  });
});

当我运行代码时,queryHelper.getAllResultsForQuery函数似乎运行得很好,但Promise似乎在该函数完成之前触发,因此控制台输出如下所示:

//in queryHelper.js
exports.getAllResultsForQuery = function(query, console) {
  var resultArray = [];
  var limit = 1000;

  var sendQuery = function(skip) {
    if (skip) {
      query.greaterThan("createdAt", skip);
    }
    query.limit(limit);
    query.ascending("createdAt");
    query.find().then(function (newResults) {
      receivedResults(newResults);
    }, function (error) {
      return Parse.Promise.error(new Error("query failed after " + resultArray.length + " results, error:" + error));
    });
  };

  var receivedResults = function(received) {
    resultArray = resultArray.concat(received);
    console.log("Got " + received.length + " objects, now at " + resultArray.length);
    if (received.length == limit) {
      sendQuery(received[received.length-1].createdAt);
    } else {
      console.log("returning from getAllResults...");
      return resultArray;
    }
  };

  sendQuery();
};

我在某个地方犯了一个菜鸟错误吗?

1 个答案:

答案 0 :(得分:0)

getAllResultsForQuery应该直接返回一个promise。我标记了额外的回报。

exports.getAllResultsForQuery = function(query, console) {
    var resultArray = [];
    var limit = 1000;

    var sendQuery = function(skip) {
        if (skip) {
            query.greaterThan("createdAt", skip);
        }
        query.limit(limit);
        query.ascending("createdAt");
        return query.find().then(function (newResults) { //return added here
            return receivedResults(newResults); //return added here
        }, function (error) {
            return Parse.Promise.error(new Error("query failed after " + 
                resultArray.length + " results, error:" + error));
        });
    };

    var receivedResults = function(received) {
        resultArray = resultArray.concat(received);
        console.log("Got " + received.length + " objects, now at " + 
          resultArray.length);
        if (received.length == limit) {
            return sendQuery(received[received.length-1].createdAt).then(function (received) {
                //added code here
                resultArray.concat(results);
                return resultArray;
            }); //return added here
        } else {
            console.log("returning from getAllResults...");
            return resultArray;
        }
     };
     return sendQuery(); //return added here
};