通过链接克服了 Cloud Code Parse Limit 1000? [英] Cloud Code Parse Limit 1000 overcome with Chaining?

查看:15
本文介绍了通过链接克服了 Cloud Code Parse Limit 1000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下函数来确定用户在记分板中的排名.

I have the following function below which I am using to determine a users rank in a scoreboard.

Parse.Cloud.define("getUserGlobalRank", function(request, response) 
{
    var usernameString = request.params.username;
    var scoreAmount = request.params.score;

    var globalRankQuery = new Parse.Query("scoreDB");
        globalRankQuery.greaterThanOrEqualTo("score",scoreAmount);  
        globalRankQuery.descending("score");
        globalRankQuery.limit("1000");
        globalRankQuery.count(
        {
        success: function(count);
        {
            response.success(count);                
        },

........

但是,如果满足该条件的条目超过 1000 个,这将不会给出准确的响应.我想将一组 .find 方法链接在一起,然后我可以对其执行 .count .有谁知道我如何实现这一目标?如果你能提供一个代码示例就太好了!

However this will not give an accurate response if there are more than 1000 entries that meet that criteria. I would like to chain together a set of .find methods that I can subsequently perform a .count on. Does anyone have any idea how I can achieve this? If you could provide a code example that would be great!

非常感谢,詹姆斯

推荐答案

以下答案适用于 Cloud Code,因为它通过在对 getUserGlobalRank() 的连续调用中使用函数参数来跟踪计数.我已经在类似的案例中成功地使用了这种架构.

The following answer will work in Cloud Code as it keeps track of the count by using a function parameter in successive calls to getUserGlobalRank(). I have used this architecture in similar cases successfully.

正如 Gene Z. Ragan 在他的评论中提到的,当前接受的答案在 Cloud Code 中不起作用,因为没有窗口"对象.

As Gene Z. Ragan mentioned in his comment, the currently accepted answer will not work in Cloud Code because there is no 'window' object.

递归已卸载到常规 Javascript 函数的原因是因为 Parse 对 Cloud Code 函数的递归限制非常低(我已验证对 Cloud Code 函数的 >4 次递归调用将导致错误).通过在 Javascript 函数中实现递归,您可以绕过 Parse 的递归限制,只要代码在允许的时间(约 15 秒)内执行,就可以继续进行递归调用.

The reason that recursion has been offloaded to a regular Javascript function is because Parse has a very low recursion limit for Cloud Code functions (I have verified that >4 recursive calls to a Cloud Code function will result in an error). By implementing the recursion in a Javascript function, you can bypass Parse's recursive limit, and continue making recursive calls as long as the code executes in the permitted time (about 15 seconds).

Parse.Cloud.define('getUserGlobalRank', function(request, response) {
     getUserGlobalRank({'username':request.params.username, 'score':request.params.score}, {
        success: function(count) {
            response.success(count);
        },
        error: function(error) {
            response.error(error);
        }
    }); 
});

function getUserGlobalRank(request, response) {

    var usernameString = request['username'];
    var scoreAmount = request['score'];
    var count = (request['count'])? request['count'] : 0;

    var globalRankQuery = new Parse.Query("scoreDB");
    globalRankQuery.greaterThanOrEqualTo("score", scoreAmount);  
    globalRankQuery.descending("score");
    globalRankQuery.limit(1000);
    globalRankQuery.skip(count);
    globalRankQuery.find({
        success: function(results) {
            if (results.length > 0) {
                count = count + results.length;
                getUserGlobalRank({'count':count, 'username':usernameString, 'score':scoreAmount}, response);
            }
            else { // found count of users with higher ranks
                response.success(count);
            }
        },
        error: function(error) { // query error
            response.error(error);
        }
    });
}

这篇关于通过链接克服了 Cloud Code Parse Limit 1000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆