medusa icon indicating copy to clipboard operation
medusa copied to clipboard

Error: Relations [products] are not valid - Trying to expand products when calling categories api

Open moheldesoqy opened this issue 10 months ago • 8 comments

Hello!

Currently i have enabled the beta feature categories in the medusajs panel, and have added the needed categories/subcategories in my project, and have called those categories in the medusajs api.

However when i am trying to query the expand in the categories list function, in here for example:

  async getProductCategories(authorization: string) {
    try {
      const category =
        await this.medusaApi.productCategories.list({
          expand: "products"
        },{
          Authorization: `${authorization}`
        });

      console.log(category);
      const categoryString = safeJsonStringify(category);
      return JSON.parse(categoryString);
    } catch (error) {
      console.log(JSON.stringify(error));
    }
  }

I get this error in the medusa logs:

error:   Relations [products] are not valid
Error: Relations [products] are not valid
    at validateRelations (/Users/moh/Projects/medusa_project/node_modules/@medusajs/medusa/dist/utils/get-query-config.js:156:15)
    at prepareRetrieveQuery (/Users/moh/Projects/medusa_project/node_modules/@medusajs/medusa/dist/utils/get-query-config.js:143:9)
    at attachListOrRetrieveConfig (/Users/moh/Projects/medusa_project/node_modules/@medusajs/medusa/dist/api/middlewares/transform-query.js:171:74)

However i do see a product_categories_product table in the postgres database that has both the category id and the product id

any ideas?

moheldesoqy avatar Apr 23 '24 12:04 moheldesoqy

Could you give more context such as your medusajs version?

ByronKweh avatar Apr 25 '24 11:04 ByronKweh

Here are my dependencies, it should the most recent version:

    "@mantine/core": "^7.8.0",
    "@medusajs/admin": "7.1.11",
    "@medusajs/cache-inmemory": "^1.8.9",
    "@medusajs/cache-redis": "^1.8.9",
    "@medusajs/event-bus-local": "^1.9.8",
    "@medusajs/event-bus-redis": "^1.8.11",
    "@medusajs/file-local": "^1.0.3",
    "@medusajs/icons": "^1.2.1",
    "@medusajs/medusa": "^1.20.4",
    "@medusajs/ui": "^1.0.0",

moheldesoqy avatar Apr 25 '24 11:04 moheldesoqy

Could we have a small reproduction of this repo so we could see how you're using the medusa API?

ByronKweh avatar Apr 25 '24 11:04 ByronKweh

Yes this is happening here too, products is not retrieved by default. Adding expand=products throws the error Requested fields [products] are not valid for this call /admin/product-categories?expand=products and /admin/product-categories/id?expand=products

Adding products to a category is successful but it does not return the existing products or added products. You have to use the store apis to see the products in the category.

package versions

"@medusajs/admin": "7.1.12",
 "@medusajs/cache-inmemory": "^1.8.10",
 "@medusajs/cache-redis": "^1.9.0",
 "@medusajs/event-bus-local": "^1.9.8",
 "@medusajs/event-bus-redis": "^1.8.12",
 "@medusajs/file-local": "^1.0.3",
 "@medusajs/medusa": "1.20.4",
 "@tanstack/react-query": "5.28.9",
 "body-parser": "^1.20.2",

kevinrobert3 avatar Apr 25 '24 17:04 kevinrobert3

Yeah i figured, it's strange because there is already a table with their relation in the database so i assumed it does work

moheldesoqy avatar Apr 25 '24 17:04 moheldesoqy

Also do you have the mpath field returned? The docs https://docs.medusajs.com/modules/products/categories#productcategory-entity-overview say it is one of the important fields but I don't see it in response

kevinrobert3 avatar Apr 25 '24 18:04 kevinrobert3

Just checked, no it doesnt return in my response nor do some of the data mentioned in the documentation response json

moheldesoqy avatar Apr 25 '24 18:04 moheldesoqy

It might have something to do with the allowedAdminProductCategoryRelations on the admin list product categories endpoint not including the products relation. https://github.com/medusajs/medusa/blob/f03a8222534ce98ad3ea84316fa44a5ce392e1b9/packages/medusa/src/api/routes/admin/product-categories/index.ts#L136C14-L136C50

DidierGuyon avatar Apr 26 '24 07:04 DidierGuyon

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 3 days.

github-actions[bot] avatar Oct 14 '24 02:10 github-actions[bot]