Vins
Vins

Reputation: 1934

Parse server query each with useMasterKey parameter

I'm migrating from Parse to Parse server. Most of my code is made without promises. For this to work, I have to send the parameter: useMasterKey: true (where necessary) for each query / save.

For find and get queries or fetch objects, I have no problems, example:

Parse.com (find)

query.find({
    success: function(results) {
    //...

Parse Server (find)

query.find({useMasterKey: true
    }).then(function(results) {
    //....

Parse.com (fetch)

user.fetch({
    success: function(user) {
    //...

Parse Server (fetch)

user.fetch({useMasterKey: true,
    success: function(user) {
    //....

The problem is with each functions:

Parse.com (each)

query.each(function(comment) {
    //...

Parse Server (each)

query.each({useMasterKey: true
      }).then(function(comment) {
      //....

It does not work.

Thanks

Upvotes: 5

Views: 2825

Answers (2)

LuisHolanda
LuisHolanda

Reputation: 11

The each method of a query support useMasterKey, it's passed as an argument after the callback function, that will be executed for each result of the query.

The syntax is:

query.each(function (object, error) {
        // Your function code
    }, {
        useMasterkey: true
})

Where object is a result for the query and error is a possible error that happened.


But, as shown here, it's better to just use useMasterKey for when you're actually changing something in the database:

query.each(function (object, error) {
    object.destroy({
        success: function (object) {
            console.log("Successfully destroyed object.")
        },
        error: function (error) {
            console.log("Error: " + error.code + " - " + error.message)
        },
        useMasterKey: true
   })
})

Upvotes: 1

HorseloverFat
HorseloverFat

Reputation: 3336

Although the docs don't suggest that the useMasterKey option is supported for an each query, having tested and verified myself it is in fact possible. Syntax as follows:

query.each(callback, {useMasterKey: true})

Where callback is a function that is called for each result of the query.

Upvotes: 3

Related Questions