adrianhajdin / project_e_commerce

This is a code repository for the corresponding video tutorial. In this video, we're going to build a fully functional eCommerce application using commerce.js.
https://jsmastery.pro
1.88k stars 506 forks source link

The given data was invalid #36

Open andrewnashed opened 3 years ago

andrewnashed commented 3 years ago

Hi guys i'm creating an e-commerce with CommerceJs and NextJS when handling payment i get this error

message: "The given data was invalid." type: "unprocessable_entity" line_items.item_7RyWOwmK5nEa2V.quantity: ["Requested quantity is not available."]

i tried to change my code like the docs but no luck

ashrithyvs commented 3 years ago

I'm having the same issue

rajmahil commented 3 years ago

Getting the same issue

ARNAV-GHATE commented 2 years ago

@andrewnashed have u got any solution for it

ARNAV-GHATE commented 2 years ago

Please feel free to post if u get any solution

andrewnashed commented 2 years ago

@ARNAV-GHATE this solution worked for me

function sanitizedLineItems(lineItems) { return lineItems.reduce((data, lineItem) => { const item = data; let variantData = null; if (lineItem.selected_options.length) { variantData = {

      };
    }
    item[lineItem.id] = {
      quantity: lineItem.quantity,
      variants: variantData,
      ...data
    };

  return item;

  }, {});
};

and then you would use the function in the orderData like so

    `line_items: sanitizedLineItems(checkoutToken.live.line_items)`
ARNAV-GHATE commented 2 years ago

@ARNAV-GHATE this solution worked for me

function sanitizedLineItems(lineItems) { return lineItems.reduce((data, lineItem) => { const item = data; let variantData = null; if (lineItem.selected_options.length) { variantData = { [lineItem.selected_options[0].group_id]: lineItem.selected_options[0].option_id, }; } item[lineItem.id] = { quantity: lineItem.quantity, variants: variantData, ...data };

  return item;

  }, {});
};

and then you would use the function in the orderData like so

    `line_items: sanitizedLineItems(checkoutToken.live.line_items)`

where we have to use this code in which component??? not able to get u?

andrewnashed commented 2 years ago

@ARNAV-GHATE this solution worked for me function sanitizedLineItems(lineItems) { return lineItems.reduce((data, lineItem) => { const item = data; let variantData = null; if (lineItem.selected_options.length) { variantData = { [lineItem.selected_options[0].group_id]: lineItem.selected_options[0].option_id, }; } item[lineItem.id] = { quantity: lineItem.quantity, variants: variantData, ...data };

  return item;

  }, {});
};

and then you would use the function in the orderData like so

    `line_items: sanitizedLineItems(checkoutToken.live.line_items)`

where we have to use this code in which component??? not able to get u?

inside the payment form

eramadani3 commented 2 years ago

Im getting a memory leak when it says error invalid data, anyone know why? Its in the address form

portercol commented 2 years ago

That fix above did not work for me as the customer data was not sent.