Reputation: 2807
I am trying to update a user (Which is not the current user). I figured that you have to use cloud code to make the update.
I have created the cloud function below to update the selected user. I am trying to add meetingId's to an array property which belongs to User class.
Cloud Code:
Parse.Cloud.define('updateUser', function(request, response) {
var userId = request.params.userId,
meetingId = request.params.meetingId;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userId });
user.addUnique("meetingsArray", meetingId)
user.save(null, {userMasterKey:true}).then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
Objective-C //meetingId - is obtained from Meeting Object.
[PFCloud callFunctionInBackground:@"updateUser" withParameters:@{@"objectId":user.objectId, @"meetingsArray":meetingId} block:^(NSString *result, NSError *error)
{
if (!error) {
NSLog(@"%@",result);
}else if(error){
NSLog(@"%@", error);
}
}];
When I run the app - I get the following error code:
Error Domain=Parse Code=141 "The operation couldn’t be completed. (Parse error 141.)" UserInfo=0x1704f2780 {code=141, temporary=0, error={"code":201,"message":"missing user password"}, originalError=Error Domain=NSURLErrorDomain Code=-1011 "The operation couldn’t be completed. (NSURLErrorDomain error -1011.)"}
I'm new to Cloud code - i just basically want to update the array that belongs to the selected user and add a meetingId to it. Any help would be greatly appreciate it.
Upvotes: 4
Views: 666
Reputation: 62686
There are a few problems with the code that will prevent it from working:
the cloud code expects the meeting id parameter to be named @"meetingId"
, so change the parameters passed to @{@"objectId":user.objectId, @"meetingId":meetingId}
use Parse.User
, not '_User'
to extend user.
get - don't build - the user being updated
Summing up...
Parse.Cloud.define('updateUser', function(request, response) {
var userId = request.params.userId;
var meetingId = request.params.meetingId;
var query = new Parse.Query(Parse.User);
query.get(userId).then(function(user) {
user.addUnique("meetingsArray", meetingId);
return user.save(null, {useMasterKey:true});
}).then(function(user) {
response.success(user);
}, function(error) {
response.error(error);
});
});
Upvotes: 4
Reputation: 147
I was still getting the error after implementing @danh's answer. I needed to add
Parse.Cloud.useMasterKey()
in the .js file then everything worked perfect!
Upvotes: 1