I have this piece of code:
function createNewProduct() {
const newProduct = {
'name': $w('#iptNomePacco').value,
'description': JSON.stringify(secondRepeaterData, ["nome", "quantity"]),
'price': totaleDaPagare,
'productType': 'physical'
};
createCustomProduct(newProduct)
.then((result) => {
const src = 'https://static.wixstatic.com/media/d6e20a_b6708b98d1944a91a3344bf2b89f3e58~mv2.jpg';
addMediaToProduct(result._id, [{ src }])
.then((response) => {
$w('#shoppingCartIcon').addToCart(result._id)
.then(() => {
console.log("Product added");
})
.catch((error) => {
console.log(error);
});
$w('#goToCheckout').show();
idProdotto = result._id;
let customID = Math.floor(Math.random() * 90000) + 10000000;
let toInsert = {
'_id': customID.toString(),
'nomePacco': $w('#iptNomePacco').value,
'cosaContiene': JSON.stringify(secondRepeaterData, ["nome", "quantity"]),
'idProdottoStore': idProdotto,
'prezzo': totaleDaPagare.toString()
}
$w('#txtAggiuntoAlCarrello').show();
wixData.insert('cesteCustom', toInsert)
.catch((err) => {
console.log(err)
});
})
.catch((error) => {
console.error('Error in createCustomProduct', error);
})
})
.catch((error) => {
console.error('Error in createCustomProduct', error);
});
}
which creates a new product, adds it to the cart and then saves everything in a database.
It works correctly but addToCart () sometimes gives me an error and sometimes it doesn’t, I can’t really understand, the code doesn’t change but sometimes it doesn’t add to the cart and sometimes it does.
Can anyone help me figure out how to correct this?