Skip to content
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

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

Open
moheldesoqy opened this issue Apr 23, 2024 · 8 comments

Comments

@moheldesoqy
Copy link

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?

@ByronKweh
Copy link

Could you give more context such as your medusajs version?

@moheldesoqy
Copy link
Author

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",

@ByronKweh
Copy link

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

@kevinrobert3
Copy link

kevinrobert3 commented Apr 25, 2024

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",

@moheldesoqy
Copy link
Author

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

@kevinrobert3
Copy link

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

@moheldesoqy
Copy link
Author

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

@DidierGuyon
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants