Reputation: 563
My application displays data to my users via the Parse.com. I am using their iOS sdk with their cloud code. I am required to use cloud code to add an object to my user. What I'm looking for is something like:
[PFCloud callFuction:@"addObjectToUser" withParameters:params];
And my parameters would be the userID and the the object ID of the object I want associated with that user ID.
How would I achieve this in JavaScript?
Upvotes: 0
Views: 416
Reputation: 8287
In order to change anything on a user object in parse (without being changed by the user themselves) it must be done through cloud code and by using the useMasterKey()
. Assuming that you have set up your params
like this:
NSDictionary *params = @{userID: @"12345", objectID: @"67890"};
You can save objects to a user with the master key like this:
//Add Object To User's "objectsAddedToUser" array
Parse.Cloud.define("addObjectToUser", function(request, response) {
// (1)
var userID = request.params.userID;
var objectID = request.params.objectID;
// (2)
var query = new Parse.Query(Parse.User);
query.equalTo("userID", userID);
query.first().then(function (user) {
// (3)
Parse.Cloud.useMasterKey();
// (4)
user.add("objectsAddedToUser", objectID);
// (5)
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
}, function (error) {
response.error(error);
});
});
(1) Get the parameters that you passed in.
(2) Query the database and retrieve the user that needs to be updated.
(3) IMPORTANT Use the master key to unlock privileges.
(4) Add the objectID
to the user.
(5) Save to Parse.
Upvotes: 2
Reputation: 14487
Adding cloud function is explain in detail here, there is an example as well which explain how you can write java-script function to manipulate or add data.
Upvotes: 1