addToCart HELP :)

Hello all,
I am attempting to add an #addtocart function to my #repeater but am unable to figure out where I went wrong. My goal is to have the query result added to the #cart with the #onclick function for “button2”. Here is my code:

wixData.query("batteryfitment") 
                .include("title") 
                .contains("year", VINInfo.Results[8].Value) 
                .and(wixData.query("batteryfitment") 
                    .contains("make", VINInfo.Results[5].Value) 
                    .and(wixData.query("batteryfitment") 
                        .contains("model", VINInfo.Results[7].Value))) 
                .find() 
                .then((results1) => { 
 **let**  results = results1; 
 **let**  product = results.items[0].title[0]._id; 
                    console.log(results1); 
                    $w("#text1").text = results.items[0].title[0].name; 
                    $w("#text3").html = results.items[0].title[0].description; 
                    $w("#image8").src = results.items[0].title[0].mainMedia; 
 **if**  ($w("#image8").hidden) { 
                        $w("#image8").show();} 
                    $w("#text5").text = "$" + "" + results.items[0].title[0].price; 
 **let**  productID = results.items[0].title[0]._id; 
                    $w("#repeater1").show(); 
                    $w("#button2").show(); 
                       $w('#button2').onClick(()=>{$w('#shoppingCartIcon1').addToCart("product");}); 

.

Hi Brittani,

I’m seeing something at a glance here. The addToCart function should have the actual ID from the stores/products collection. Is that what your variable named ‘product’ is? In other words, do you have that ID in the title field of batteryfitment?

Another thing that I noticed was that it should be .addToCart(product) without the quotes around it as product is a variable. If you put quotes around it, Corvid will treat it as a literal string of “product” and not the variable that is supposed to be holding the product ID.

Hi @tony-brunsman ,
Thank you for your response. The ‘product’ mentioned should result to the queried products id ( results.items[0].title[0]._id ). I am now receiving the following error code: FetchError: [object Object]

I have made adjustments to the code which now reads as:

 import  { getVINInfo }  from  'backend/VINModule';  import  { wixData }  from  'wix-data';  import  {fetch}  from  'wix-fetch';  $w.onReady( function  () {  //TO DO: Write Your Page Related Code Here: });   export function  button1_click(event, $w) {  //Add your code for this event here:     getVINInfo($w(" [#vininput](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.vininput) ").value)         .then(VINInfo => {             console.log(VINInfo)             $w(" [#results](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.results) ").text = "Vehicle Specific Battery for Your" + " " + VINInfo.Results[8].Value + "  " + VINInfo.Results[5].Value + "  " + VINInfo.Results[7].Value;              wixData.query("batteryfitment")                 .include("title")                 .contains("year", VINInfo.Results[8].Value)                 .and(wixData.query("batteryfitment")                     .contains("make", VINInfo.Results[5].Value)                     .and(wixData.query("batteryfitment")                         .contains("model", VINInfo.Results[7].Value)))                 .find()                 .then((results1) => {   let  results = results1;                       console.log(results1);                     $w(" [#text1](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.text1) ").text = results.items[0].title[0].name;                     $w(" [#text3](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.text3) ").html = results.items[0].title[0].description;                     $w(" [#image8](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.image8) ").src = results.items[0].title[0].mainMedia;   if  ($w(" [#image8](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.image8) ").hidden) {                         $w(" [#image8](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.image8) ").show();}                     $w(" [#text5](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.text5) ").text = "$" + "" + results.items[0].title[0].price;                     $w(" [#repeater1](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.repeater1) ").show();                     $w(" [#button2](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.button2) ").show();                     $w(" [#button2](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.button2) ").onClick(()=>{$w(' [#shoppingCartIcon1](https://www.wix.com/corvid/forum/search/posts%3Fquery=.num.shoppingCartIcon1) ').addToCart([   {  "productID": "results.items[0].title[0]._id]",  "quantity": 1   }])     .then( () => {     console.log("Product added");   } )   . catch ( (error) => {     console.log(error);});     } );                            }                      )                 })}