4

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.

2 Answers 2

4

There are a few problems with the code that will prevent it from working:

  1. the cloud code expects the meeting id parameter to be named @"meetingId", so change the parameters passed to @{@"objectId":user.objectId, @"meetingId":meetingId}

  2. use Parse.User, not '_User' to extend user.

  3. 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);
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! thank you for clarifying how to use cloud function. You code should work - I'm getting the following error. "The operation couldn’t be completed. (Parse error 141.)" UserInfo=0x1702f8080 {code=141, temporary=0, error={"code":101,"message":"Object not found."}, --- not sure why this is happening because the user i'm trying to update does exist and has a property (array) to store the meetingid's.
So you've examined the userid passed to the cloud and confirmed that there's a user with that id in the data browser?
correct -im checking that the user i want to update exists before it get passed in to [ PFCloud callFunctionInBackground....];
Okay, but please do the check manually. Log the id, and log the meetingId. Then find them in the data browser. (If you do the check in code, there's a chance that there's a mistake in the code).
you have typo in your code: Should be useMasterKey instead of userMasterKey
1

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!

2 Comments

I had the same, but there is a typo in @danh's answer. Should be useMasterKey instead of userMasterKey
Sorry about this. Fixed my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.