如何从非用户更新PFUser属性?

时间:2016-04-04 15:07:25

标签: javascript swift parse-platform

我想更新名为Rating的PFUser属性。此评级属性将从发布评级分数的非用户获取其数据。但是,我不知道如何解决此问题,因为出于安全原因,Parse不允许您将数据从非用户发布到PFUser列.I发现了这篇文章(Saving attributes on a user fetched from a query (i.e. not on the currentUser)) 问题在概念上类似,但在实施中却没有。这是我已经改变但我不熟悉JavaScript,所以我目前正在努力使用Cloud Code实现该功能。

原创功能

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

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

user.set('new_col', newColText);

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

我尝试了什么

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

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

user.set('Rating', rating); //Is 'Rating' the PFUser field name?

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

调用功能

[PFCloud callFunction:@"editUser" withParameters:@{
    @"userId": @"someuseridhere",
    @"newColText": @"new text!"
}];

我的快速版本

private func didFinishTouchingCosmos(rating: Double)
{
    let query = PFUser.query()

    if let objectaidi = self.tutor?.objectId
    {
          query?.getObjectInBackgroundWithId(objectaidi, block: { (tutorobject, errar) in
            if let input = self.ratinginput
            {
               PFCloud.callFunctionInBackground("editUser", withParameters: ["userId": objectaidi, "newColText": "new text!"])
                PFCloud.callFunctionInBackground("hello", withParameters: nil) {
                    (response: AnyObject?, error: NSError?) -> Void in
                    let responseString = response as? String
                    print(responseString)
                }
                print("AYYAYYAA")
            }
          })
    }       
}

我从Javascript代码中获得的错误:

  

未捕获的SyntaxError:main.js中的意外标记ILLEG:7

请帮忙!

1 个答案:

答案 0 :(得分:1)

Got it! The answer is very simple :

Calling function :

private func didFinishTouchingCosmos(rating: Double)
{
    let query = PFUser.query()

    if let objectaidi = self.tutor?.objectId
    {
            query?.getObjectInBackgroundWithId(objectaidi, block: { (tutorobject, errar) in
            if let input = self.ratinginput
            {
                PFCloud.callFunctionInBackground("hello", withParameters: nil) {
                    (response: AnyObject?, error: NSError?) -> Void in
                    let responseString = response as? String
                    print(responseString)
                }
                PFCloud.callFunctionInBackground("editUser", withParameters: ["userId": objectaidi, "rating": input.rating]) {
                    (response: AnyObject?, error: NSError?) -> Void in
                    let responseString = response as? String
                    print(responseString)
                }
                print("AYYAYYAA")

            }

          })
    }

And the Javascript code is :

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

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

               user.set('Rating', rating);

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