1

我对如何从云代码更新用户感到困惑。如果有人可以帮助我解决这个问题,我将不胜感激。

我将用于此示例的云代码是:

云码

Parse.Cloud.define("like", function(request, response) {
    var userID = request.params.userID;

    var User = Parse.User();
    user = new User ({ objectId: userID });

    user.increment("likes");

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

});

从 iOS 调用

[PFCloud callFunctionInBackground:@"like" withParameters:@{@"userID": self.selectedFriendID} block:^(id object, NSError *error) {

}];

问题

user = new User ({ objectId: userID });

1)上面代码中的“新”是创建一个“全新”用户,还是它“抓取”一个现有用户objectId以便可以对其进行更新user.save

2)如果后者是正确的,那么是否可以从列以外的objectId列中“抓取”用户,或者从列中抓取userID

例如:

user = new User ({ userID : userID });
4

2 回答 2

3

这一行:

user = new User ({ objectId: userID });

创建具有已知 ID 的对象的实例,它仅适用于objectId. 您对该对象所做的任何更改都将保留,但不会更改其他数据(例如,您不会不小心将其他列清空)。

相反,如果您想通过电子邮件获取用户,则必须进行查询:

var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('email', userEmail);
userQuery.first().then(function(user) {
    if (user) {
        // do something with the user here
        user.set('new_col', 'new text');
        user.save().then(function() {
            response.success();
        });
    } else {
        // no match found, handle it or do response.error();
    }
});

// the above code is async, don't put anything out here and expect it to work!
于 2014-08-07T08:54:20.867 回答
2

在云代码中检索用户我会做的是 query.get(request.params.userID.... 我相信这会返回用户对象。我不知道你是否可以对其他列执行此操作。有很多不过,云代码文档中有关于此的内容。这是我用于编辑用户的云代码功能,如果有帮助的话:

Parse.Cloud.define("editUser", function(request, response) {
  //var GameScore = Parse.Object.extend("SchoolHappening");
  // Create a new instance of that class.
  //var gameScore = new GameScore();
Parse.Cloud.useMasterKey();

  var query = new Parse.Query(Parse.User);
query.get(request.params.myUser, {
  success: function(myUser) {
    // The object was retrieved successfully.
    myUser.set("cabinetPosition", request.params.myPosition);
    // Save the user.
      myUser.save(null, {
        success: function(myUser) {
          // The user was saved successfully.
          response.success("Successfully updated user.");
        },
        error: function(gameScore, error) {
          // The save failed.
          // error is a Parse.Error with an error code and description.
          response.error("Could not save changes to user.");
        }
      });

  },
  error: function(object, error) {
    // The object was not retrieved successfully.
    // error is a Parse.Error with an error code and description.
  }
});

});
于 2014-08-06T21:36:20.050 回答