Skip to content

📙 beforeDelete Trigger Example #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dblythy opened this issue Oct 25, 2020 · 3 comments · Fixed by #782
Closed

📙 beforeDelete Trigger Example #781

dblythy opened this issue Oct 25, 2020 · 3 comments · Fixed by #782

Comments

@dblythy
Copy link
Member

dblythy commented Oct 25, 2020

Link to section:
https://docs.parseplatform.org/cloudcode/guide/#delete-triggers

What is the issue?
The example is:

Parse.Cloud.beforeDelete("Album", (request) => {
  const query = new Parse.Query("Photo");
  query.equalTo("album", request.object);
  query.count()
    .then((count) => {
      if (count > 0) {
        throw "Can't delete album if it still has photos.";
    })
    .catch((error) {
      throw "Error " + error.code + " : " + error.message + " when getting photo count.";
    });
});

And states:

If the function throws, the Album object will not be deleted, and the client will get an error. Otherwise, the object will be deleted normally.

-The function isn't async, or doesn't return a promise.
-Syntax error after the first throw (no closing bracked)
-Syntax error after .catch (no =>)
-Catching the first error will return 'Error undefined : undefined when getting photo count.'

I might be wrong, but wouldn't the delete happen prior to the error throwing?

Can you propose a solution?
What changes do you think should be made? Have you considered multiple solutions? Will this be suitable for all use cases?

Parse.Cloud.beforeDelete("Album", (request) => {
  const query = new Parse.Query("Photo");
  query.equalTo("album", request.object);
  return query.count()
    .then((count) => {
      if (count > 0) {
        throw "Can't delete album if it still has photos.";
      }
    })
});

or

Parse.Cloud.beforeDelete("Album", async (request) => {
  const query = new Parse.Query("Photo");
  query.equalTo("album", request.object);
  const count = await query.count()
  if (count > 0) {
    throw "Can't delete album if it still has photos.";
  }
});
@davimacedo
Copy link
Member

I think you're right. Do you want to open a PR for that?

@dblythy
Copy link
Member Author

dblythy commented Oct 26, 2020

Yep, I ended up included the changes in the PR for the cloud validator. Is that okay, or would you like a seperate PR?

@davimacedo
Copy link
Member

No problem.

@TomWFox TomWFox linked a pull request Oct 26, 2020 that will close this issue
@TomWFox TomWFox removed a link to a pull request Oct 26, 2020
@TomWFox TomWFox linked a pull request Oct 26, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants