解析CloudCode功能不起作用。调用它时出错

时间:2015-09-12 14:49:11

标签: javascript objective-c xcode parse-platform cloud

CloudCode功能是:

Parse.Cloud.define('addFriend', function(request, response) {
   var userId = request.params.userId;

   var User = Parse.Object.extend('_User'),
       user = new User({ objectId: userId });

   var currentUser = request.user;

   var relation = user.relation("Friends");
   relation.add(currentUser);

   Parse.Cloud.useMasterKey();
   user.save().then(function(user) {
       response.success(user);
   }, function(error) {
    response.error(error)
   });
});

当我通过PFCloud调用从Xcode调用它时....我在控制台中收到下一个错误:

[Error]: ReferenceError: parent is not defined
at e.n.value (Parse.js:13:7510)
at main.js:10:14 (Code: 141, Version: 1.8.2)

这意味着实际问题是关系没有父关系。它应该已经有了用户'变量作为父变量,因为我正在调用user.relation(" Friends"),这是文档所说的内容:"你很少应该手动设置父亲,因为你是很可能会调用Parse.Object.relation(' key')。"这就是我在做的事。

更新

即使这段代码也会出现同样的错误:

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

    var currentUser = request.user;
    var relation = currentUser.relation("Friends");
    relation.add(currentUser);
});

更新2:

用户JSON

{
   "FriendRequests": {
    "__type": "Relation",
    "className": "_User"
},
   "Friends": {
    "__type": "R‌​elation",
    "className": "_User"
},
"createdAt": {
    "__type": "Date",
    "iso": "2015-09-10T17:29‌​:03.181Z"
},
"sessionToken": "r:KkmTu7xRjTEFpl0vcANVf0xXH",
"updatedAt": {
   "__type": "Dat‌​e",
   "iso": "2015-09-10T17:29:03.181Z"
 },
 "username": "bogdan12",
 "objectId": "BmbNRxCyau‌​"

}

关系JSON

{
    "__type": "Relation",
    "className": "_User"
}

1 个答案:

答案 0 :(得分:1)

尝试更换:

var User = Parse.Object.extend('_User'),
    user = new User({ objectId: userId });

使用:

    user = new Parse.User.createWithoutData(userId);

编辑:

它适用于我...这是我尝试过的:

云:

Parse.Cloud.define('addFriend', function(request, response)
{
    var currentUser = request.user;
    var userId = request.params.userId;
    var user = new Parse.User.createWithoutData(userId);

    var relation = user.relation("Friends");
    relation.add(currentUser);

    Parse.Cloud.useMasterKey();
    user.save().then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error)
    });
});

客户:

[PFCloud callFunctionInBackground:@"addFriend" withParameters:@{@"userId":@"s8BMHtnqSZ"} block:^(id object, NSError *error) {
    if (!object || error) {
        LogError(error, @"Calling addFriend failed.");
    } else {
        LogInfo(@"Calling addFriend succeeded.");
    }
}];

也许您应该在此处提交错误报告:https://www.parse.com/help