如何从非用户更新 PFUser 属性?
Posted
技术标签:
【中文标题】如何从非用户更新 PFUser 属性?【英文标题】:How to update PFUser attribute from a non user? 【发布时间】:2016-04-04 15:07:24 【问题描述】:我想更新名为 Rating 的 PFUser 属性。此评级属性将从发布评级分数的非用户那里获取其数据。但是,我不知道如何解决这个问题,因为出于安全原因,Parse 不允许您将数据从非用户发布到 PFUser 列。我看到这个帖子(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!"
];
我的 swift 版本
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 代码中得到的错误:
Uncaught SyntaxError: Unexpected token ILLEGAL in main.js:7
请帮忙!
【问题讨论】:
我很确定您需要先查询用户,您不能仅使用其 objectId 实例化用户对象,因此在您的云代码中,查询用户,然后在该对象上设置新的评级属性并然后将其保存在成功块中 介意展示一个例子吗?我的 JavaScript 很弱。 【参考方案1】:知道了!答案很简单:
调用函数:
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")
)
而 Javascript 代码是:
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)
);
);
【讨论】:
以上是关于如何从非用户更新 PFUser 属性?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中同步检索 PFUser objectForKey
如何将用户从非 UIView 类推送到 ViewController [重复]