parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Parse Server Cloud Code - Destroy with masterKey

Open sanergulec opened this issue 10 years ago • 7 comments

Hello,

I am trying to delete an object with the code below, but I tried lots of things and could not make it work. Am I doing something wrong in the code below?

            var notifications = Parse.Object.extend('notifications');
            var query3 = new Parse.Query(notifications);
            query3.equalTo('useridActionTaker', req.object.get('userid'));
            query3.find({useMasterKey: true}).then(function(result3) {


                  if ( result3.length > 0 ) {

                      return result3[0].destroy(null, { useMasterKey: true });

                  }

                  else {

                  }

              });

sanergulec avatar Mar 08 '16 07:03 sanergulec

Can you add some logging to the destroy call to determine what the error message it?

drew-gross avatar Mar 08 '16 07:03 drew-gross

I found that when I set the CLP write permission to public it is working, but when I remove the write permission, it is not. Am I using the "useMasterKey" wrong? Actually the read permission will be public, but the write permission wont be.

sanergulec avatar Mar 08 '16 07:03 sanergulec

No, that should be the correct way to use master key. Can you check if the error from the destroy call is a permission error vs. some other error?

drew-gross avatar Mar 08 '16 08:03 drew-gross

Hello drew-gross,

I tried to add the error log as below, and see the error, but it somehow started to work:) I guess it is because I removed the "null" part in the destroy while adding the success and error logs.

            var notifications = Parse.Object.extend('notifications');
            var query3 = new Parse.Query(notifications);
            query3.equalTo('useridActionTaker', req.object.get('userid'));
            query3.find({useMasterKey: true}).then(function(result3) {

                  if ( result3.length > 0 ) {

                      return result3[0].destroy({ useMasterKey: true,

                        success: function(result3)
                        {
                        },                  
                        error: function(error)
                        {
                          console.error(error.message);
                        }

                      });


                  }

                  else {

                  }

            });

sanergulec avatar Mar 08 '16 11:03 sanergulec

I realize this was closed, but I came across the same issue and can confirm that removing null solved the problem.

If the class has a restricted CLP in place this works:

object.destroy({useMasterKey: true});

but this does not:

object.destroy({null, useMasterKey: true});

brendankopp avatar May 29 '17 18:05 brendankopp

Same here....had to remove the null. Seems that unlike user.save there does not seem to be a first parameter in user.destroy so:

user.destroy(null, {useMasterKey:true}); // Does not work user.destroy({useMasterKey:true}); // Works!

OpConTech avatar Mar 08 '18 17:03 OpConTech

That makes sense, as the first parameter of save() is a dictionary of keys to update and their value. With destroy(), you're not updating any values before doing the destroy.

If you look at the documentation for destroy(), it has only one argument: An options parameter, which is where you could add success / error handlers, as well as the session / master key. http://parseplatform.org/Parse-SDK-JS/api/v1.11.1/Parse.Object.html#destroy

Whereas the save() documentation shows that there are several appropriate combinations of arguments: http://parseplatform.org/Parse-SDK-JS/api/v1.11.1/Parse.Object.html#save

JacobJT avatar Mar 08 '18 19:03 JacobJT