Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving attributes on a user fetched from a query (i.e. not on the currentUser)

I am interested in saving attributes to users in the database based on actions that are performed by my currentUser. Based on the following code, I get the error message "User cannot be saved unless they have been authenticated via logIn or signUp"

NSString *IDToFetch = @"some_id";
PFQuery *query = [PFUser query];
[query whereKey:@"user_id" equalTo:IDToFetch];
PFUser *foundUser = (PFUser *)[query getFirstObject];

foundUser[@"new_column"] = @"Some text";
[foundUser saveEventually]; //here an exception is thrown

I was wondering if there is a work around to this where I could save attributes to the foundUser without having to log that user on. Thanks for your help!

like image 212
daspianist Avatar asked Sep 06 '13 03:09

daspianist


2 Answers

If you want to update a user that is not currently the logged in user you will need to call Parse using the master-key.

You can do this from CloudCode for example;

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)
    });
});

and calling it from your iOS project;

[PFCloud callFunction:@"editUser" withParameters:@{
    @"userId": @"someuseridhere",
    @"newColText": @"new text!"
}];
like image 130
hank Avatar answered Nov 10 '22 04:11

hank


OK Providing this as an answer but basically not recommending it! I think the cloud code route is cleaner at the end of the day. But, this is arguably easier..

You can create another table that pairs with each user.

eg.

table name

user_extra_details

fields

User

Score

so other users can edit this particular field on user_extra_details

But as you suspect this can lead to lots of extra queries but it's not impossible, just... inconvenient. So here's how you'd access your data in that set up.

PFQuery *userQuery = [PFQuery queryWithTable:@"Users"];
[userQuery whereKey:Some Key equalTo:some value];

PFQuery *userDetails [PFQuery queryWithTable:@"user_extra_details"];
[userDetails whereKey:@"User" inQuery:userQuery];
[userDetails includeKey:@"User"];
[userDetails fetch];

The thinng you have to be wry about is that duplicate details objects will create multiple results obviously. So I suggest doing a delete and insert vs doing an update of the extra_detail object to help combat any erroneous duplicates.

like image 2
Pork 'n' Bunny Avatar answered Nov 10 '22 03:11

Pork 'n' Bunny