Parse Server Cloud Code - Destroy with masterKey
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 {
}
});
Can you add some logging to the destroy call to determine what the error message it?
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.
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?
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 {
}
});
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});
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!
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