Parse.com云作业承诺与嵌套查询

时间:2014-03-08 21:50:53

标签: javascript parse-platform promise

我在Parse.com上有一个游戏,用户提交作品并且其他玩家投票。

我正在尝试编写一个云后台作业来更新我的所有用户的帐户,其中的投票总数是每个提交的投票总数的总和。

我已经尝试了5种不同的方法,无法运行嵌套查询(更不用说更新和保存)了。

以下是文档和有用的博文......

https://parse.com/docs/js_guide#promises-creation

http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/

这是我最新的,非常感谢任何帮助...

编辑:感谢@axymous让我看清楚,我需要明确创建并立即返回承诺,然后在回调中解决它。

Parse.Cloud.job("calculateUserTotals", function(request, status) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.find().then(function(users) {
        var promises = [];
        _.each(users, function(user) {
            promises.push((function(user){
                var promise = new Parse.Promise();
                console.log(user.toJSON().objectId);  // user objectId is correct here
                var submissionQuery = new Parse.Query('submission');
                submissionQuery.equalTo('user', user.toJSON().objectId);
                submissionQuery.find({  // this doesnt seem to ever run, i have tried .each as well
                    success: function(submissions) {
                        var votes = 0;
                        for (var i=0; i<submissions.length; i++) {
                            var submission = submissions[i];
                            votes = votes + submission.get('votes').length;
                        }
                        user.set('submission_count', submissions.length);
                        user.set('vote_count', votes);
                        user.save({
                            success: function(user) {
                                promise.resolve();
                            }
                        });
                    },
                    error: function(error) {
                        status.error(error);
                    }
                });
                return promise;
            })(user));
        });
        return Parse.Promise.when(promises);
    })
    .then(function() {
        status.success('leaderboard updated.');
    });
});

1 个答案:

答案 0 :(得分:5)

我需要显式创建并立即返回promise,然后在回调中解析它。谢谢@axymous。

Parse.Cloud.job("calculateUserTotals", function(request, status) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.find().then(function(users) {
        var promises = [];
        _.each(users, function(user) {
            promises.push((function(user){
                var promise = new Parse.Promise();
                console.log(user.toJSON().objectId);  // user objectId is correct here
                var submissionQuery = new Parse.Query('submission');
                submissionQuery.equalTo('user', user.toJSON().objectId);
                submissionQuery.find({  // this doesnt seem to ever run, i have tried .each as well
                    success: function(submissions) {
                        var votes = 0;
                        for (var i=0; i<submissions.length; i++) {
                            var submission = submissions[i];
                            votes = votes + submission.get('votes').length;
                        }
                        user.set('submission_count', submissions.length);
                        user.set('vote_count', votes);
                        user.save({
                            success: function(user) {
                                promise.resolve();
                            }
                        });
                    },
                    error: function(error) {
                        status.error(error);
                    }
                });
                return promise;
            })(user));
        });
        return Parse.Promise.when(promises);
    })
    .then(function() {
        status.success('leaderboard updated.');
    });
});