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.

Home Page:https://jsmastery.pro

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The given data was invalid

andrewnashed opened this issue · comments

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

I'm having the same issue

Getting the same issue

@andrewnashed have u got any solution for it

Please feel free to post if u get any solution

@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)`

@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?

@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

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

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