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

查看:69
本文介绍了可以通过链接克服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天全站免登陆