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



我以为我已经弄清楚了这个承诺的事情,但是这个从查询中获取所有结果的函数,无论 1000 个项目的限制如何,都无法正常工作。谁能向我解释一下我在这里出错的地方?谢谢!

此云作业在我的主文件中.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.js:

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

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

"Here we go…";
"Finished the search!";
"Got 1000 objects, now at 1000";
"Got 1000 objects, now at 2000";
"Got 1000 objects, now at 3000";
"Got 245 objects, now at 3245";
"returning from getAllResults…";

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

getAllResultsForQuery应该直接返回一个承诺。我标记了添加的回报。

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

最新更新