Add to cart code problem

I am adding products to cart using code. The first product gets added seamlessly the way I desire, but the second product never adds and show me this in google chrome console log:

FetchError: [object Object]
at new a (https://static.parastorage.com/services/wixstores-client-worker/1.165.0/unified-viewer.bundle.min.js:1:456913)
at eval (https://static.parastorage.com/services/wixstores-client-worker/1.165.0/unified-viewer.bundle.min.js:1:457559)
at eval (https://static.parastorage.com/services/wixstores-client-worker/1.165.0/unified-viewer.bundle.min.js:1:4668)
at Object.eval [as next] (https://static.parastorage.com/services/wixstores-client-worker/1.165.0/unified-viewer.bundle.min.js:1:4773)
at i (https://static.parastorage.com/services/wixstores-client-worker/1.165.0/unified-viewer.bundle.min.js:1:3544)

Code I used is this:

// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixData from ‘wix-data’;
// …
$w.onReady( function () {
//TODO: write your page related code here…

});

let cart = “”;
let fabric = “”;

export function button5_click(event) {
//Add your code for this event here:
wixData.query(“Stores/Products”)
.contains(“name”, $w(“#input1”).value)
.find()
.then( (results) => {
let newitem = results.items[0];
console.log(newitem)
let name = newitem.name;
console.log(name);
$w(“#text35”).text = name;
let url = newitem.mainMedia;
console.log(url);
$w(“#image2”).src = url;
let price = newitem.price;
console.log(price);
let newprice = price.toString()
$w(“#text37”).text = newprice;
cart = newitem._id;
console.log(cart)
fabric = newitem.productOptions.Fabric.choices[0].description;
console.log(fabric)
let options1 = newitem.productOptions.Color.choices;
console.log(options1)
let options2 = newitem.productOptions.Size.choices;
console.log(options2)

let options1l = options1.length;
let length1 = options1l;
let value1 = options1;
let options2l = options2.length;
let length2 = options2l;
let value2 = options2;

let test = ;
for ( let i = 0; i < length1; i++) {
test[i] = {
label: value1[i].value,
value: value1[i].value
}
$w(“#dropdown1”).options = test;
}
let test1 = ;
for ( let j = 0; j < length2; j++) {
test1[j] = {
label: value2[j].value,
value: value2[j].value
}
$w(“#dropdown2”).options = test1;
}

} ); 

}

export function button6_click(event) {
//Add your code for this event here:
$w(‘#shoppingCartIcon1’).addToCart(cart, $w(“#dropdown4”).value, {
“choices”: {
“Size”: $w(‘#dropdown2’).value,
“Color”: $w(‘#dropdown1’).value,
“Fabric”: fabric
}

}).then( () => { 
console.log("Product added"); 

} )
. catch ( (error) => {
console.log(error);
} );

}

Please help me out. All is working great and this is something which is just keeping me apart from the end.

Hey Logan! Thanks for your reply, but it is not a router problem. Tried it from multiple other devices and networks.

Solved!

Hello Sagar Jain,
can you please share your solution? It would be great help since encountered similar problem.

Hey! No problem. I figured out that the dropdown is creating all sores of issue. Actually, the dropdown value does not change if page is not refreshed. So, After each add to cart I set the selectedindex of the dropdown to null/undefined so that I can reselect the value for the dropbox and it works flawlessly.

Here, I have attached the code. It is always a pleasure to help:

// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixData from ‘wix-data’;
// …
$w.onReady( function () {
//TODO: write your page related code here…

});

export function button5_click(event) {
//Add your code for this event here:
wixData.query(“Stores/Products”)
.contains(“name”, $w(“#input1”).value)
.find()
.then( (results) => {
let newitem = results.items[0];
let name = newitem.name;
$w(“#text35”).text = name;
let url = newitem.mainMedia;
$w(“#image2”).src = url;
let price = newitem.price;
let newprice = price.toString()
$w(“#text37”).text = newprice;
$w(“#text38”).text = newitem._id;
$w(“#text39”).text = newitem.productOptions.Fabric.choices[0].description;
let options1 = newitem.productOptions.Color.choices;

let options2 = newitem.productOptions.Size.choices;

let options1l = options1.length;
let length1 = options1l;
let value1 = options1;
let options2l = options2.length;
let length2 = options2l;
let value2 = options2;

let test = ;
for ( let i = 0; i < length1; i++) {
test[i] = {
label: value1[i].value,
value: value1[i].value
}

$w("#dropdown1").options = test; 
} 

let test1 = ;
for ( let j = 0; j < length2; j++) {
test1[j] = {
label: value2[j].value,
value: value2[j].value
}
$w(“#dropdown2”).options = test1;
}

} ); 

}

export function button6_click(event) {
//Add your code for this event here:
console.log($w(“#text38”).text);
console.log($w(“#text39”).text);
console.log($w(‘#dropdown2’).value);
console.log($w(‘#dropdown1’).value);

$w(‘#shoppingCartIcon1’).addToCart($w(“#text38”).text, $w(“#dropdown3”).value, {
“choices”: {
“Size”: $w(‘#dropdown2’).value,
“Color”: $w(‘#dropdown1’).value,
“Fabric”: $w(“#text39”).text
}

}).then(()=>{ 

$w(“#dropdown1”).selectedIndex = undefined;
$w(“#dropdown2”).selectedIndex = undefined;
$w(“#dropdown3”).selectedIndex = undefined;

})
. catch ( (error) => {
console.log(error);
} );

}