Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Cart ajax API change.js sending 404 inside a loop.

Cart ajax API change.js sending 404 inside a loop.

Not applicable

I am trying to remove cart items by `change.js` Ajax API. First I loop throw some specific cart items. and inside the loop, I am calling the API. All calls are working fine but the last. When the last elements of the array are coming, the API response status is 404. It is saying that it is a bad request. The message "no valid ID or row parameters".

 

My code is

 

for (const variant of variants) {
      const formData = {
        line: parseInt(variant.id),
        quantity: variant.qty,
      };
      
      try {
        const response = await fetch(url, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(formData),
        });

        const data = await response.json();
        responses.push(data);
      } catch (error) {
        console.error("Error:", error);
        responses.push(null); // You can handle the error as needed
      }
    }

 

 Note: The other call is working. For example - when I am trying to increase or decrease the quantity. Even a few calls are working inside the loop.

Replies 3 (3)

Liam
Community Manager
3108 342 884

Hi Abdullah21673,

 

The error message "no valid ID or row parameters" from the change.js API usually indicates that the cart does not have a line item that matches the line or id parameter you passing.

Here are a few things you could look into:

  1. Make sure the line parameter matches the index of the line item in the cart. This is 1-based, meaning the first item in the cart has a line value of 1, not 0. If you're using the zero-based index from your variants array, you'll need to add 1 to get the correct line value.

  2. Verify that the variant.id values in your variants array correspond to the id or line value of the items in the cart. Remember, id in the cart is not the variant's id, it is the unique id of the cart item.

  3. Ensure that the quantity you're trying to set is not greater than the available inventory.

  4. If the last item in the array is causing the error, it might be a timing issue. The change API may not have finished processing the previous request before the next one is sent. In case, you could try adding a delay between each request.

  5. Verify that all items in the variants array have valid id and qty properties.

  6. Ensure you're not trying to remove an item that has already been removed. If you're reducing the quantity of an item to 0 (effectively removing it), any subsequent requests to change that item will return a 404 error because it no longer exists in the cart.

Look into the above and let us know if you're still seeing issues.

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Not applicable

Thanks. The problem is solved

Not applicable

Hello, everything seems to be in order here, and all functions are operating smoothly. However, it's crucial to highlight that I invoke an API within a loop. During the loop execution, it systematically eliminates items from the shopping cart. To illustrate, suppose cart line number 1 is removed. In the database, the second cart item then assumes the position of cart line number 1.

The challenge arises when attempting to remove cart line number 2 within the loop. In reality, there is no longer a line number 2 in the database because what used to be line number 2 has now become line number 1. I've conducted tests to confirm this hypothesis, and it aligns with my expectations.

I've since resolved the issue by recalling the API with the new line ID. This ensures that the correct line is targeted for removal. If my explanation is unclear to anyone or if there is interest in delving deeper into this matter, please don't hesitate to reach out to me for further clarification.