我从未使用过云代码/javascript,我正在尝试编写一些解析云代码来使用传递给云函数的 objectId 查找用户,然后更新包含朋友的用户关系并最终保存该用户。
下面是我使用的函数:
Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) {
Parse.Cloud.useMasterKey();
var fromUserObjectId = request.params.fromUserObjectId;
var acceptingUser = request.params.user;
var query = new Parse.Query(Parse.User);
// find the user the request was from using the objectId
query.get(fromUserObjectId, {
success: function(user) {
var fromUser = user
var relation = fromUser.relation("friends");
relation.add(acceptingUser);
fromUser.save({
success: function() {
response.success("Successfully saved the users relation")
},
error: function() {
response.error("Save failed");
}
});
},
error: function() {
response.error("Save failed");
}
});
});
我设法使用 Parse 文档将其拼凑在一起。但我真的没有很好地遵循它。从未使用过 javascript,并且发现语法令人困惑。
然后我用
//fromUser is a PFUser object defined further up
[PFCloud callFunctionInBackground:@"addFriendToFriendsRelation" withParameters:@{@"fromUserObjectId" : fromUser.objectId} block:^(id object, NSError *error) {
}
但是,每当调用此函数时,我都会得到一个成功/错误,而不是称为错误。虽然我在函数中调用 response.success 和 response.error 所以我不知道为什么会这样?任何人都可以伸出援助之手吗?
编辑:在做了更多搜索之后,看起来 response.success 和 response.error 应该只调用一次,所以我修改了我的函数看起来像这样:
arse.Cloud.define("addFriendToFriendsRelation", function(request, response) {
Parse.Cloud.useMasterKey();
var fromUserId = request.params.fromUserObjectId;
console.log("fromUserId:");
console.log(fromUserId);
var acceptingUser = request.params.user;
console.log("acceptingUser:")
console.log(acceptingUser);
var query = new Parse.Query(Parse.User);
query.get(fromUserId, {
success: function(user) {
console.log("found user:");
console.log(user);
var fromUser = user;
var relation = fromUser.relation("friends");
relation.add(acceptingUser);
console.log("added accepting user to relation");
fromUser.save({
success: function() {
response.success("successfully saved user")
},
error: function() {
response.error("error saving user");
}
});
console.log("found a user");
},
error: function() {
console.log("error finding user");
}
});
});