Parse.com Cloud Code上的多对多关系

时间:2015-10-12 20:51:59

标签: javascript ios web-applications parse-platform cloud-code

我有一个班级“团队”,可以有很多用户。

每次将用户添加到团队时,我都需要创建一个from / to关系。每个用户都可以对所有队友进行评分和评分。

这是我的云代码。我似乎无法让这个工作。

FigureButton:
    text: '1'
FigureButton:
    text: '2'

向团队添加播放器效果很好,它在客户端运行。

数据浏览器上的类设置: Team Class Rate Class

2 个答案:

答案 0 :(得分:1)

你的课程是"费率"但是你使用的是这段代码:

 var Rate = Parse.Object.extend("Rate"),
                        rate = new Rate();

更改为

 var Rate = Parse.Object.extend("Rates"),
                        rate = new Rate();

此外,请确保在添加关系之前创建对象。

                        var Rate = Parse.Object.extend("Rate"),
                        rate = new Rate();

                           [.... MOVED BELOW .....}

                        rate.save(null, {
                            success: function(_team) {
                           [.... now create and save relations ...]
                            },

答案 1 :(得分:0)

这样解决了。

唯一令我恼火的是,如果我尝试这个,我会收到一个错误:

“fromRelation期待_User,但得到_Team”,为什么?

player = results[i];
var fromRelation = rate.relation('from');
fromRelation.add(player);

我不得不创建2个新的vars,fromPlayer和toPlayer,虽然它对我来说似乎很愚蠢但无论如何都有效。

Parse.Cloud.define("updateTeamPlayers", function(request, response) {
    var Team = Parse.Object.extend("Team"),
        team = new Team();
        team.id = request.params.id;

    var relation = team.relation("players"),
        query = relation.query();

    query.find({
        success: function(results) {
            var i, j, player, teammate, queryArray = [], teammateRes = results;

            for (i = 0; i < results.length; i++) {
                player = results[i];

                // for each player add a new Rate relation (from/to)
                for (j = 0; j < teammateRes.length; j++) {
                    teammate = teammateRes[j];

                    // if it's same player, escape relation
                    if (player == teammate) { continue; }

                    var Rate = Parse.Object.extend("Rates"),
                        rate = new Rate();

                    // create the relation from this player
                    var fromPlayer = new Parse.User();
                        fromPlayer.id = player.id;

                    var fromRelation = rate.relation('from');
                        fromRelation.add(fromPlayer);

                    // to every other player on the team
                    var toPlayer = new Parse.User();
                        toPlayer.id = teammate.id;

                    var toRelation = rate.relation('to');
                        toRelation.add(toPlayer);

                    queryArray.push(rate);
                }
            }

            Parse.Object.saveAll(queryArray, {
                success: function(_allsaved) {
                    response.success('rate save success:' + _allsaved);
                },
                error: function(error) {
                    response.error('rate save error:' + error + ' =|= ' + error.message);
                }
            });

            // end suucess
        },
        error: function() {
            response.error("team failed");
        }
    });
});