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

前端 未结 2 1885
我在风中等你
我在风中等你 2020-12-08 09:06

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 err

相关标签:
2条回答
  • 2020-12-08 09:16

    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.

    0 讨论(0)
  • 2020-12-08 09:25

    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!"
    }];
    
    0 讨论(0)
提交回复
热议问题