在PARSE中运行_.each

时间:2015-06-14 12:53:28

标签: javascript parse-platform promise

我有一个名为“circle”的表,它代表邮件列表。 Circle可以在inviteUser中包含用户ID,也可以在inviteCircle中包含circle id。 我需要获得包含特定用户的所有圈子ID(在代码中:1-9)。 例如,如果studantA在CircleA中 CircleA在CircleB中 所以我需要将studantA作为输出CircleA和CircleB。

我尝试找到所有圈子,他们通过while和each来相互结合,但是作业的状态是:无法连接到Cloud Code。 如何在找不到结果之前运行此查询?

Parse.Cloud.job("fillMemberInCircles", function (request, status) {

    var studentId = "123456789";
    var output = [];
    var lowLayer = [];
    var highLayer = [];

    // Get circles when the student appears in column invitedUsers
    var invitedUsers = new Parse.Query("circles");
    invitedUsers.equalTo("invitedUsers", studentId);
    invitedUsers.find({
        success: function (invitedUsersResults) {

            if (invitedUsersResults.length > 0) {
                for (var i = 0; i < invitedUsersResults.length; i++) {
                    output.push(invitedUsersResults[i].id);
                    lowLayer.push(invitedUsersResults[i].id);
                }

                var counter = lowLayer.length;
                var _ = require('underscore.js');
                
                while (counter > 0) {
                    //var _ = require('underscore.js');
                    _.each(lowLayer, function (currCircle) {

                        var invitedCirclesQuery = new Parse.Query("circles");
                        invitedCirclesQuery.equalTo("invitedCircles", currCircle);
                        invitedCirclesQuery.find().then(function (results) {
                            if (results.length != 0) { // item exists
                                for (var j = 0; j < results.length; j++) {
                                    if (output.indexOf(results[j].id) == -1) {
                                        output.push(results[j].id);
                                        highLayer.push(results[j].id);
                                    }
                                }
                            }
                        }).then(function () {
                            counter--;
                            if (counter == 0) {
                                if (highLayer.length == 0) {
                                    console.log("OK");
                                    lowLayer = [];
                                    status.success("SUCSESS: " + output.length.toString());
                                } else {
                                    lowLayer = highLayer;
                                    counter = lowLayer.length;
                                    highLayer = [];
                                }
                            }
                        });
                    });
                }
            }

            //status.success("SUCSESS: " + output.length.toString());
        },
        error: function () {
            status.error("[fillMemberInCircles] There was some error with queryMajors");
        }
    });

});

0 个答案:

没有答案