【问题标题】:Parse Cloud Code Promise not returning correctly解析云代码承诺未正确返回
【发布时间】:2017-11-19 04:54:53
【问题描述】:

我以为我已经解决了这个 Promises 问题,但是无论 1000 项限制如何,这个从 Query 中获取所有结果的功能都无法正常工作。任何人都可以向我解释我要去哪里错了吗?谢谢!

此 Cloud Job 在我的 main.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…";

我是不是在某个地方犯了新手错误?

【问题讨论】:

    标签: javascript parse-platform promise parse-cloud-code


    【解决方案1】:

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

    【讨论】:

    • 谢谢!我以为我以前尝试过,但我只是测试了它以确定。控制台输出现在的顺序正确,但没有找到结果。也许是一个不同的问题?
    • @guthook 哪里没有返回结果?通常你没有全局数组,而是在内部连接并在最后返回。
    • @neils-steenbeek 查询在 receivedResults 函数中返回 0 个结果,因此控制台读取“得到 0 个对象,现在为 0”。我尝试切换它,使其不使用全局数组,但仍然没有运气。我可能会与托管我的 Parse 数据库的人核实一下,看看它是否可能是服务器的错误。
    • @guthook 在“if == limit”块中,resultArray 未丰富。可能这就是问题所在。总体而言,拥有全局 resultArray 的概念很糟糕。您应该改为连接所有返回值。
    • 谢谢尼尔斯!我发现查询没有返回任何结果的原因是我需要在查找请求中使用主密钥。但我也会对全局数组进行一些更改,并改用 concat 命令。所以你第一个答案是对的。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2020-03-03
    相关资源
    最近更新 更多