NotToBrag
NotToBrag

Reputation: 665

Parse, Function only works for the current user

The following function seems to only work for my Current user, any other ObjectId that I type into the input will return a Post (Bad Request) error in the console.

var query =  new Parse.Query(Parse.User);
var userInput = $('#inputObject').val();
query.equalTo("objectId", userInput);
query.first({
    success: function(result) {
        console.log(result.id);
        result.set('money', 20);
        result.save();
    },
    error: function(error) {
        console.log("None found.");
    }
});

Upvotes: 1

Views: 81

Answers (1)

Dehli
Dehli

Reputation: 5960

The problem with this is that the Parse user object is only modifiable by the current user. You can get around this by creating a Cloud Code function, which modifies the money value for the intended user. Note, the cloud code function must call Parse.Cloud.useMasterKey(); Source

CloudCode Function:

Parse.Cloud.define("setMoney", function(request, response) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("objectId", request.params.userId);
    query.first({
        success: function (result) {
            console.log(result.id);
            result.set('money', 20);
            result.save();
            response.success();
        },
        error: function (error) {
            console.log("None found.");
            response.error(error);
        }
    });
});

JavaScript Call

Parse.Cloud.run("setMoney", {userId: userInput}, {
    success: function(result) {
        // Success
    },
    error: function(error) {
        // Error
    }
});

Upvotes: 1

Related Questions