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

[Bug]: Add to Cart Workflow broken since last update #10644

Closed
fxmb opened this issue Dec 17, 2024 · 7 comments
Closed

[Bug]: Add to Cart Workflow broken since last update #10644

fxmb opened this issue Dec 17, 2024 · 7 comments

Comments

@fxmb
Copy link

fxmb commented Dec 17, 2024

Package.json file

The commit 8 hours ago broke the add to cart workflow I believe:

   const cart = transform({ cartQuery }, ({ cartQuery }) => {
      return cartQuery.data[0]
    })

This query result from here: https://github.com/medusajs/medusa/blob/develop/packages/core/core-flows/src/cart/workflows/add-to-cart.ts#L47

returns the first cart that was created in the database.

I always get this error when trying to add to the cart using the addToCart Workflow:

Cart cart_01J801RD5JEWMPDS0V1AZZM0FP is already completed.

However, this cart (ending on FP) is not the one I am sending to the server, its an old cart from another customer...

This is the first cart ever created in our db, hence I assume some query is failing, sorted by date and returning the first [0] cart since its sorted by created_at.

Node.js version

20

Database and its version

16

Operating system name and version

..

Browser name

No response

What happended?

see above

Expected behavior

see above

Actual behavior

see above

Link to reproduction repo

no link

@olivermrbl
Copy link
Contributor

olivermrbl commented Dec 17, 2024

@fxmb, thanks for the report. How are you consuming the workflow? Its input type changed in the latest release. You should pass the cart_id instead of cart now.

I'll add this to the release notes.

@fxmb
Copy link
Author

fxmb commented Dec 17, 2024

Ha that fixed it! Thanks for pointing this out so quickly

For everyone else:

 const workflowInput = {
    items: line_items,
    cart
  };

  await addToCartWorkflow(req.scope).run({
    input: workflowInput,
  });

becomes:

    items: line_items,
    cart_id: cart.id,
  };

  await addToCartWorkflow(req.scope).run({
    input: workflowInput,
  });

@Thisisjuke
Copy link

Thisisjuke commented Dec 18, 2024

Hey,
At the moment, we are using Medusa v2.0.7 before upgrading to last version in the next weeks.
We had thus bug because of :
image

@medusajs/core-flows was not a dependency we had in our package.json, as we used this one: https://github.com/tinloof/medusa-dtc-starter-munchies/blob/main/storefront/package.json

Is it normal that we can have "@medusajs/framework": "2.0.7", and still have issues about 2.1.2 ? Did we missed something ?

We had the same error as @fxmb about already completed, and after wiping the database and starting fresh on data, now we have:
image
image

http:    GET /store/carts/cart_01JFD1KHDA6M00EC1YWZ8KYKW0?fields=%2Bitems%2C%20%2Bregion%2C%20%2Bitems
│ .product.%2A%2C%20%2Bitems.variant.image%2C%20%2Bitems.variant.%2A%2C%20%2Bitems.thumbnail%2C%20%2Bite
│ ms.metadata%2C%20%2Bpromotions.%2A%2C ← - (200) - 34.256 ms
│ error:   Value for LineItem.cart_id is required, 'undefined' found
│ entity: LineItem {

Our way to fix it force the version in our yarn.lock like this:

"@medusajs/core-flows@npm:2.0.7, @medusajs/core-flows@npm:^2.0.7":
  version: 2.0.7
  resolution: "@medusajs/core-flows@npm:2.0.7"
  dependencies:
    json-2-csv: "npm:^5.5.4"
  peerDependencies:
    "@medusajs/framework": ^2.0.0
    awilix: ^8.0.1
  checksum: 10c0/0e2e1a17a9f7442345afd179c4be8ce423f8b49bc4097e04b25a6178de2233669436b514e0056a80e6ecbc0131a60ba5a863aa3270b3a0e77a3b00a74d2d291a
  languageName: node
  linkType: hard

By putting this at the root of our yarn workspace :

"resolutions": {
  "@medusajs/medusa/@medusajs/core-flows": "2.0.7"
}

😿
Do you know a proper way to not have issues related to futur version in our actual codebase & version of ALL the @medusajs packages ?

@mohamadfala
Copy link

The exact same issue is happening for me.
Running v2.1.1.

@thoward27
Copy link

thoward27 commented Jan 4, 2025

I'm seeing the same on a pretty fresh install on v2 running on Vercel, with a cloudflare tunnel to my backend.

Oddly enough, I am completely unable to reproduce the behavior locally. I tried setting my API server to production mode, but that isn't doing it. The requests themselves do look different, the one from my local dev includes a CSRF token in the cookies, but the one from vercel does not.

I've no idea what to do to resolve this, should I try upgrading? Would a stack trace of the issue be helpful? Is there a good way to debug this?

It must have to do with how I'm communicating with my API server. When MEDUSA_BACKEND_URL points to https://api.server.com, I get this:

http:    GET /store/customers/me?fields=%2Aorders ← - (401) - 27.845 ms
error:   Value for LineItem.cart_id is required, 'undefined' found
...
http:    POST /store/carts/cart_01JGSGPXC8RD2R524WXTW79TR3/line-items ← - (500) - 160.077 ms

When it's pointed to http://localhost:9000

I get

http:    GET /store/customers/me?fields=%2Aorders ← - (401) - 96.512 ms
http:    POST /store/carts/cart_01JGSGPXC8RD2R524WXTW79TR3/line-items ← - (200) - 2125.345 ms

@olivermrbl
Copy link
Contributor

Do you know a proper way to not have issues related to futur version in our actual codebase & version of ALL the https://github.com/medusajs packages ?

@Thisisjuke, we intentionally keep all packages on the same version to avoid issues related to version mismatches. This is the recommended way to deal with this.

@mohamadfala, can you create a new issue detailing your problem. Please include a minimal reproduction.

@thoward27, this sounds like your remote server might have a version mismatch. Can I get you to ensure all packages have been upgraded to the same version and try again? If the issue persists, please create a new issue.

I will close this for now, as OPs problem has been resolved.

@thoward27
Copy link

For anyone with the same issue, I can confirm that I can successfully add things to my cart after upgrading all of my packages to their latest semver-compliant versions in both my backend and store.

I'm using the Next.js starter and did have to implement medusajs/nextjs-starter-medusa#391 after upgrading everything.

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

6 participants