将PFObject保存到Cloud Code中的关系(Parse.com)

时间:2014-12-21 22:42:05

标签: parse-platform cloud-code

我一直试图解决这个问题很长一段时间,但我很难过。我正在构建一个带有Parse.com后端的应用程序,我正在尝试实现此功能,当用户A尝试向用户B发送游戏请求时,用户A和用户B都在PFRelation中获取游戏。一旦他发起请求,我已经完成了将游戏保存到用户A(当前用户)关系的部分:

NSString *challengePuzzleSize = [NSString stringWithFormat: @"%ld", (long)self.sizeSegmentedControl.selectedSegmentIndex + 5];
self.aNewMatch = [PFObject objectWithClassName:@"Messages"];
[self.aNewMatch setObject:file forKey:@"file"];
[self.aNewMatch setObject:fileType forKey:@"fileType"];
[self.aNewMatch setObject:self.opponent.objectId forKey:@"opponentIDs"];
[self.aNewMatch setObject:self.opponent.username forKey:@"opponentName"];
[self.aNewMatch setObject:[[PFUser currentUser] objectId] forKey:@"senderID"];
[self.aNewMatch setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[self.aNewMatch setObject:self.opponent forKey:@"receiver"];
[self.aNewMatch setObject:challengePuzzleSize forKey:@"puzzleSize"];
[self.aNewMatch setObject:[PFUser currentUser] forKey:@"sender"];

[self.aNewMatch saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred." message:@"Please try sending your message again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alertView show];
    }

    else {
        PFRelation *currentGamesSender = [self.currentUser relationForKey:@"currentGames"];
        NSLog(@"opponent: %@", self.opponent);

        [currentGamesSender addObject:self.aNewMatch];

        [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                NSLog(@"Error %@ %@", error, [error userInfo]);
            }

            else {
                NSLog(@"WORKS");
            }
        }];

我知道我不能对用户B做同样的事情,因为他不是当前用户,而Parse因安全原因限制了保存非当前用户。所以,我试图通过Cloud Code来做到这一点,这是我在浏览解决方案时推荐的另一个人。我将此作为目标C中的调用:

[PFCloud callFunction:@"addCurrentGame" withParameters:@{@"userId": self.opponent.objectId, @"currentGameId": self.aNewMatch.objectId}];

但我不熟悉Javascript,我一直在试图弄清楚如何将PFObject保存到用户B的关系中,这对我不起作用。这是代码:

Parse.Cloud.define("addCurrentGame", function(request, response) {
Parse.Cloud.useMasterKey();

var receiver = null;
var currentGame = null;

queryReceiverUser = new Parse.Query("User");
queryCurrentGame = new Parse.Query(“Messages”);
var receiverUserId = request.params.userId;
var currentGameId = request.params.currentGameId;

queryCurrentGame.get( currentGameId, {
    success: function(_currentGame) {
        currentGame = _currentGame;

    error: function(error) {
        console.log("Error retrieving user in query!");
    }
});

queryReceiverUser.get( receiverUserId, {
    success: function(_receiver) {
        receiver = _receiver;

        var currentGames = receiver.relation(“currentGames”);
        currentGames.add(currentGame);

        receiver.save(null, {
        success: function() { console.log("Saved receiver”); },
                                          error: function(err) { console.log("Error saving receiver, " + err.code); }
                                          });

        response.success("success");

    },

    error: function(error) {
        console.log("Error retrieving user in query!");
    }
});
});

我完全不确定如何在Javascript中创建PFObject游戏并将其保存到用户B关系(我的代码不正确)。如果有人能帮我解决这个问题,那就太棒了。

1 个答案:

答案 0 :(得分:1)

Parse.Cloud.define('addCurrentGame', function(request, response) {
    Parse.Cloud.useMasterKey();
    var queryReceiverUser = new Parse.Query(Parse.User);
    var queryCurrentGame = new Parse.Query('Messages');
    var receiverUserId = request.params.userId;
    var currentGameId = request.params.currentGameId;
    return Parse.Promise.when([queryReceiverUser.get(receiverUserId), queryCurrentGame.get(currentGameId)])
        .then(function(user, game) {
            gameRelation = user.relation('currentGames');
            gameRelation.add(game);
            return user.save(null, {userMasterKey: true})
        }).then(function() {
            response.success('success');
        }, function() {
            response.error('error');
        });
});