0
async function unsubscribeUserHandler() {
  const unsubscribe = await fetch("/api/stripe-sessions/cancel-subscription", {
    method: "PATCH",
    body: JSON.stringify(),
    headers: {
      "Content-Type": "application/json",
    },
  });
  const data = await unsubscribe.json();
  if (!unsubscribe.ok) {
    Toast.fire({
      icon: "error",
      title: `${data.message}`,
    });
  } else {
    Toast.fire({
      icon: "success",
      title: `${data.message}`,
    });
  }
}

async function deleteUserHandler() {
  const deleteUser = await fetch("/api/user/delete-account", {
    method: "DELETE",
    body: JSON.stringify(),
    headers: {
      "Content-Type": "application/json",
    },
  });
  const data = await deleteUser.json();
  if (!deleteUser.ok) {
    Toast.fire({
      icon: "error",
      title: `${data.message}`,
    });
  } else {
    Toast.fire({
      icon: "success",
      title: `${data.message}`,
    });
  }
}

const deleteAccount = async () => {
    try {
        await unsubscribeUserHandler();
        await deleteUserHandler();
    } catch (err) {
        console.error('ERROR@@@@@!!!',err);
    }
}

const Settings = () => {
  return <DeleteAccount onDeleteAccount={deleteAccount} />;
};

As shown here, I want to unsubscribe.. only after the unsub, then run delete handler.

I have issues where It only runs one of the handlers and not the other. Are there other ways to do this?

have tried:

.then(() => deleteUserHandler())

and

.then(deleteUserHandler)

above doesn't make call to /api/user/delete-account,

only to unsubscribe.

17
  • 1
    In .then(deleteUserHandler()) you are calling deleteUserHandler instantly. Try removing the brackets .then(deleteUserHandler) Commented Mar 16, 2022 at 22:42
  • @jkoch just tested it-- it unsubscribes the user on Stripe, though doesn't delete the user from DB. Which means it's not handling deleteUserHandler Commented Mar 16, 2022 at 22:51
  • Could you take a look on your browsers development tools and check if there are any errors on the console and if the call to /api/user/delete-account happens? Commented Mar 16, 2022 at 22:56
  • @jkoch call to /api/user/delete-account doesn't happen, only /api/stripe-sessions/cancel-subscription Commented Mar 16, 2022 at 23:02
  • 1
    @Phil -- ended up solving the issue which was unsubscribe was throwing error bc it was not supposed to be patch to unsubscribe user thx for helping Commented Mar 17, 2022 at 1:47

1 Answer 1

1

This is wrong:

const deleteAccount = () => unsubscribeUserHandler()
    .then(deleteUserHandler())
    .catch(console.error);

You aren't passing deleteUserhandler to then(), you are immediately calling it and pass the result to then().

To fix, lose the parenthesis:

const deleteAccount = () => unsubscribeUserHandler()
    .then(deleteUserHandler)
    .catch(console.error);

Or use an arrow function:

const deleteAccount = () => unsubscribeUserHandler()
    .then(() => deleteUserHandler())
    .catch(console.error);

Or better yet:

const deleteAccount = async () => {
  try {
    await unsubscribeUserHandler();
    await deleteUserHandler();
  } catch (err) {
    console.error(err);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried the last solution and it's only calling /api/stripe-sessions/cancel-subscription, not the /api/user/delete-account-- for which I'm assuming it's still not hitting the delete handler. For which, when I call the delete handler by itself it hits the /api/user/delete-account
@temrb if unsubscribeUserHandler does not throw an error, then deleteUserHandler will be called. If not, something else is very wrong.
u are right, there was an issue with one of my methods preventing the promise to continue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.