我想在当前用户和另一个用户(userB)之间设置一个简单的关系。我可以创建sampleRelation
,当前用户显示在数据浏览器的userB< sampleRelation
列中,但当前用户的关系列表为空,即使它应显示userB& #39;的名字。 Javascript对我来说完全不为人知,我必须使用它,因为CloudCode,我最初使用iOS SDK,我称之为CloudCode函数:
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (object) {
PFUser *user = (PFUser *)object;
[PFCloud callFunction:@"editUser" withParameters:@{@"userId": user.objectId}]; }];
这是JavaScript实现:
Parse.Cloud.define('editUser', 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("sampleRelation");
relation.add(currentUser);
currentUser.save();
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
我确定我需要以某种方式保存当前用户的关系,因此我添加了currentUser.save();
行,但没有任何改变。我无法弄清楚我错过了要实现的东西,或者现有的代码隐藏了这个bug。
我的尝试基于蒂莫西的答案。
Parse.Cloud.define('editUser', 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("sampleRelation");
relation.add(currentUser);
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.get(request.params.userId)
.then(function (user) {
var relation = user.relation("sampleRelation");
relation.add(request.user);
// chain the promise
return user.save();
).then(function (user) {
response.success(user);
}, function (error) {
response.error(error);
});
这是我得到的错误:
Update failed with Could not load triggers. The error was Uncaught SyntaxError: Unexpected token . in main.js:28
第二次更新
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.get(request.params.userId)
.then(function (user) {
var relation = user.relation("sampleRelation");
relation.add(request.user);
// chain the promise
return user.save();
}).then(function (user) {
response.success(user);
}, function (error) {
response.error(error);
});
答案 0 :(得分:4)
您没有更改currentUser
,因此调用currentUser.save()
将无法执行任何操作。
此外,在获取现有用户时,您应该执行以下操作:
Parse.Cloud.define('editUser', function(request, response) {
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.get(request.params.userId)
.then(function (user) {
var relation = user.relation("sampleRelation");
relation.add(request.user);
// chain the promise
return user.save();
}).then(function (user) {
var currentUser = request.user;
var relation = currentUser.relation("sampleRelation");
relation.add(user);
// chain the new promise
return currentUser.save();
}).then(function () {
response.success();
}, function (error) {
response.error(error);
});
});