Tom Fox
Tom Fox

Reputation: 905

Parse server useMasterKey syntax

I am using a simple query to increment the stock of a product. The query works when the class level permissions are set to public read and write however I cannot work out how to get the query to use the master key so that the class can be restricted from client-side changes. How should this be done?

itemQuery.equalTo('productName', items[count]);
itemQuery.first({
  success: function(object) {
    // Successfully retrieved the object.
    object.increment('stock', 1);
    object.save();
  },
});

Upvotes: 2

Views: 1082

Answers (1)

danh
danh

Reputation: 62676

Set the class level permissions to restrict access as you see fit, then in cloud code you have two options: (1) user master key for the whole cloud function:

Parse.Cloud.useMasterKey();
itemQuery.equalTo('productName', items[count]);
// and so on...

Or (2) better, apply master key as an option for only the action that might be restricted:

// etc
object.save(null, { useMasterKey: true });

Upvotes: 4

Related Questions