Help building array from collection results to send in an email.

I have a collection that stores a list of items a user selected. The number of items is variable. My goal is that once they submit the entry, I want to send them an email confirming the items they have selected. I can not figure out how to loop through the query and build an array to later display in the email body.

A sample email body would look something like this:


Thank you for your order. Below is a list of cards you have selected. Your order will be processed once payment has been received at the school.

CARD AMOUNT QTY TOTAL
Barnes & Noble $25 4 $100
Home Depot $25 1 $25
Cold Stone Creamery $25 1 $25

Kind regards,

Wix Code Newbie


Below is the code. First it selects data from the “cart” then inserts into the “orders” table and then removes the inserted row from the cart. The last step is to loop through the “orders” and build a string, with a line break between each row, to later display in email.

Any help would be greatly appreciated!


export function queryCartForInsert() {
      console.log('queryCartForInsert');

            wixData.query("SCRIP_CART")
              .limit(1000)
              .eq("user_id", userId)
              .contains('status', 'P')
              .ascending("vendor")
              .find()
              .then((queryResults) => {

                // get the query result items
                if(queryResults.items.length > 0) {

                   //set the ID of the record                    
                   var cartId = queryResults.items.map(z => z._id);
                   console.log("CartIds =", cartId);

                  let items = queryResults.items; 
                                    
                  for (var a = 0; a < items.length; a++) {

                    var pk = queryResults.items[a]._id;
                    console.log("PK =",pk);

                    var insertOrders = {
                      "title":          items[a].title,
                      "status":         'P',
                      "first_name":     firstName,
                      "last_name":      lastName,
                      "vendor":         items[a].vendor,
                      "category_text":  items[a].category_text,
                      "amount":         items[a].amount,
                      "quantity":       items[a].quantity,
                      "total":          items[a].total,
                      "user_id":        userId
                    };

                    console.log('InsertOrders =',insertOrders);
                  
                    wixData.insert('SCRIP_ORDERS', insertOrders).
                      then ((results) => {
                        console.log("insert results ",results); 
                    }) //close insert

                    //remove the record from the collection			
                    console.log("runDelete****");
                    wixData.remove("SCRIP_CART", pk)
                      .then(() => {
                        console.log('Deleted ID ', pk);
                        
                        //refresh the table with the updated info
                        console.log('Refresh cart');
                        queryCart();

                        //Recalcualte the totals
                        console.log('Cart Delete - Recalculate the totals');

                        $w("#tbTotalCards").value = 0;
                        $w('#tbTotalAmountDue').value = 0;
                        //sumTotals(); 

                      })
                      .catch((err) => {
                        let errorMsg = err;
                        console.log(errorMsg);
                      });

                  } // close loop

                  console.log('Inside queryCartForInsert - call queryOrders')
                  console.log('queryOrders');
      
                    //Get all items the user selected to send IN email
                    wixData.query("SCRIP_ORDERS")
                      .eq("user_id", userId)
                      .eq('status', 'P')
                      .ascending("vendor")
                      .find()
                      .then((orderResults) => {
                        // get the query result items
                        console.log('queryOrders executed =',orderResults);  

                        if(orderResults.items.length > 0) {

                          //set the ID of the record, make sure working                    
                          var ordId = orderResults.items[a]._id;
                          console.log("PK =",ordId);

                          let emailItems = orderResults.items;
                          let emailCount = orderResults.length;
                          console.log('orderRows =',orderResults.items);
                          console.log('orderCount = ',orderResults.length)

                          for (var b = 0; b < emailItems.length; b++) {
   
                            displayOrders = {
                            "vendor":         orderResults.items[b].vendor,
                            "category_text": orderResults.items[b].category_text,
                            "amount":         orderResults.items[b].amount,
                            "quantity":       orderResults.items[b].quantity,
                            "total":          orderResults.items[b].total
                           };   
                        
                           console.log('Display orders =', displayOrders);                          
                           ************  HELP ******************
                           //let tagConcat = ${$w('#tag1').value} ${$w('#tag2').value};
                           let tagConcat = displayOrders  +  displayOrders;
                           console.log('tagConcat =', tagConcat);

                         } //close loop
                      } //end if;
                      }); //close query  ();
                } 
                else 
                { 
                  console.log('No records to remove')     ;
                } //end if

              }); //close then(queryRestults)

} // close queryCartForInsert

Vicky, your basic problem is “how to format something like a table inside an email”. Answer is: being very cautious. There is a myriad of email clients out there, on different OS´s and what they support and what not is a nightmare.
I have done this several times. Using CSS-tables is not supported by all email clients. So what I do (and what seems to work as far as I could check) is revert to the lowest common denominator: html-tables.
I find some online html-editor, design an old-fashioned html-table (you know,

some stuff
) and simply insert my values between the td´s and then dump that whole table into the email where it is supposed to appear. Should work.
Hope this helps.

Very good point. I was hoping for a really simple answer that I just couldn’t see. The formatting of the list within the email was wishful thinking, I’d be thrilled if I could just output each row as text (i.e. ”4 - Barnes & Noble @ $25”) but I’m guessing I would still need to put it in an html table. I will see if I can figure that out tomorrow.

i really appreciate you taking the time to respond, thank you!

Vicki