I can't figure out how to delete a Parse.Role in cloud code. The guide says they are subclasses of Parse.Object and have all the same methods. But I get the following error why I try to delete one:
Object [object Object] has no method 'destroy'
Parse.Cloud.afterDelete("Project", function(request) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.Role);
query.equalTo("name", "hasSound_" + request.object.id);
query.find().then(function(role) {
if(typeof role === 'undefined') {
console.log("role is undefined, cannot delete");
}
else {
role.destroy();
}
}).then(function(success) {
console.log("role deleted: hasSound_" + request.object.id);
}, function(error) {
console.log("error deleting role");
});
});
I had some console.log statements verifying the "name" is correct, so the query should be finding the right object. I'm not sure what else to try.
Also, my roles are named after the ids of Projects. So each Project has a role called "hasSound_[id of project]". When I delete a project, I no longer need the role associated with it. I mean I could leave the unused projects in the database, but that seems wasteful.
find()
is fulfilled with an array of matching objects. Since your roles are unique, you can safely change find()
to be first()
. Or you can keep using find()
, but treat the result as an array.