medusa: Unable to delete a category!

Describe the bug

Getting 500 Error when sending a request to delete a Category.

System information

Node.js version: v 20.4.0 Database: postgres://localhost/medusa-starter-default Operating system: macOS 10.15.7 Browser (if relevant): Chrome 120 on macOS (Ventura)

Screenshots

sdfdfssdf dfff

Code snippets

/* delete categories */

export async function deleteAllCategoriesFromAPI() {
  const allCategories = await medusa.admin.productCategories
    .list()
    .then(({ product_categories, limit, offset, count }) => {
      console.log(
        "There are: ",
        product_categories.length,
        " product categories"
      );
      return product_categories;
    });

  allCategories.forEach(async (category) => {
    await medusa.admin.productCategories
      .delete(category.id)
      .then(({ id, object, deleted }) => {
        console.log(`Category ${id} is deleted!`);
      });
  });
}

Additional context

I guess the problem should be specific to Categories Entities, because deleting Products work pretty fine. Getting Categories also works. The problem is with deleting them.

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

yes, at-least for now until the issue is resolved. forEach loop does not wait for all the promises in the iteration to be resolved. for loop on the other hand does.

All good, looks like the rank was indeed corrupted.

Finally works! Thank you so much for help! Now the problem with corrupted ranks in my db is not an issue anymore, only if I will have another project installed, is that correct? Or you’ll fix the problem so even with a new project the problem will not persist?

fgfg

Categories are deleted sdfsdfsdf

All good, looks like the rank was indeed corrupted.

Run this in psql first:

WITH IndexedCategories AS (
    SELECT
        id,
        ROW_NUMBER() OVER (ORDER BY id) AS rank_index
    FROM
        product_category
)
UPDATE product_category
SET rank = rank_index
FROM IndexedCategories
WHERE product_category.id = IndexedCategories.id AND product_category.parent_category_id IS NULL;

and then try with the updated code:

export async function deleteAllCategoriesFromAPI() {
  const allCategories = await medusa.admin.productCategories
    .list()
    .then(({ product_categories, limit, offset, count }) => {
      console.log(
        "There are: ",
        product_categories.length,
        " product categories"
      );
      return product_categories;
    });

  for (const category of allCategories) {
    await medusa.admin.productCategories
      .delete(category.id)
      .then(({ id, object, deleted }) => {
        console.log(`Category ${id} is deleted!`);
      });
  }
}

@vasilemidrigan you need to put a semi colon in the end in psql

select id, name, rank, parent_category_id from product_category;

Thanks, can you run this on your db and show the results?

select id, name, rank, parent_category_id from product_category. The rank might’ve been corrupted.