medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.93k stars 2.6k forks source link

Get products by many IDs #4196

Closed finom closed 4 months ago

finom commented 1 year ago

Bug report

Describe the bug

We use medusa.products.list heavily with id parameter to retrieve products by IDs.

await medusa.products.list({ id: ids });

But when we have too many IDs (20+) the Medusa server begins to throw an error:

{"message":"id must be one of: String,String","details":{"String":["String validation failed: [object Object] is not a string"],"undefined":["Cannot read properties of undefined (reading 'constructor')"]}}

I suppose this is a kind of URL length limitation. Is there any way to avoid that? Maybe I can create my own API endpoint that accepts POST but instead of using query params it's going to use body?

Expected behavior

I expect no error when I want to have products extracted by their ID.

Mahmoudgalalz commented 1 year ago

@olivermrbl is this bug coming from the js sdk? right @finom could you help me to navigate to the dir that handles this call, and does medusa support this functionality as well? does this code should be working, or this feature does not exist? I can work on this

Mahmoudgalalz commented 1 year ago
route.get(
    "/:id",
    transformQuery(FindParams, {
      defaultRelations: defaultAdminProductRelations,
      defaultFields: defaultAdminProductFields,
      isList: false,
    }),
    middlewares.wrap(require("./get-product").default)
  )

as I inspected the code @olivermrbl you should send a list of ids not an object

await medusa.product.list([1,2,3,4])

Screenshot 2023-05-31 at 12 00 46 AM this what also mentioned in the docs

so this isn't a bug, the title of this issue should be change into requesting a feature if the team is willing to do so

ziitel2 commented 1 year ago

After debugging, I found that the req.query from Express are contains more than 20 indexes. It will convert to an object instead of an array. Which shown in a picture below.

image

So the validator inside transformStoreQuery as a middleware is being executed the function will then throw an error.

I'm not sure that this is the right way, To query more than 20 products at a time. Didn't see this issue being active. Oh! in my case i was trying calling GET /store/products with many IDS for showing filtered product that contains 25 product per page.

Also our filtering options come from VariantOption that is why we could not done that via API(there is not support list product from variant option) as of now.

olivermrbl commented 1 year ago

The issue with > 20 params has been solved in #4213 and will be part of next release.

sradevski commented 4 months ago

Hey, thanks for the report! Since v2 brought a lot of architectural and API changes on the backend, we will be closing this ticket since it no longer applies to our new setup, or the issue has already been fixed. If you are still facing issues with v1, please open a new ticket and we will address it as soon as possible. Thanks! 🙏