Still can’t figure this out. Appreciate whatever help you can provide.
First I do not have a members login page because it is not needed for my purposes (And setting that up is problematic for my coding level of understanding - never had any formal training). All the examples I have seen on the forum are related to Members…I don’t have members at this point in development and may never need them. My goal is to allow anyone who chooses to pick the Build A System page from the front end menu to be able to run that page, which also allows them to update a database called BASProducts. For now the code does the updating in the front end, but later can all go into the back end for security after I understand how all this works.
In the back end I am trying to clear or Truncate all records from the BASProducts database. Then in the front end, after all selections are made, the code repopulates the BASProducts and redirects to the “/quote” page. On the quote page the BASProducts selections are read and pasted to the permanent database called Request. Then, after collecting visitor information, an confirmation email of selections is sent (using sendGrid ) to the site Admin and the Visitor. All that works in the Preview but is blocked due to authorizations (I think) in Publish “live” mode.
The code I am posting here fails to redirect to the /quote page and fails to Truncate and do the updates in the BASProducts database. As you suggested I am posting now the relevant parts of my code for your review and comments. I really appreciate your feedback about anything you observe here:
BackEnd - clearDatabase.jsw
import wixData from ‘wix-data’ ;
export function runClear () {
wixData . truncate ( “BASProducts” ,{ suppressAuth : true })
. then ( () => {
console . log ( “Success! All items truncated from BASProducts” );
})
. catch ( ( err ) => {
console . log ( "error message from backend " , err );
});
return “runClean finished”
}
FrontEnd - Build A System page code
import wixData from ‘wix-data’ ;
import wixStores from ‘wix-stores’ ;
import wixLocation from ‘wix-location’ ;
import { runClear } from ‘backend/clearDatabase’ ;
$w . onReady ( function () {
//console.log("…in $w.onReady…: ")
// other unrelated code here
runClear ()
console . log ( "clear BASProducts in back end" )
// other unrelated code here
// after making all selections, the button is pressed to start the process of
// updating BASProducts
// the rest of the code below is exactly as I have coded it, sorry about the ugly console.logs and notes.
export function contactButton_click ( event ) {
//
//this function collects the selected products information from the page elements and pushes all into the “BASProducts” database fields
//
let acount = 0
narrowCollection . forEach ( function ( item , index ) {
let selItem = $w ( ‘#’ + item + ‘Text’ ). text ;
let selSku = $w ( ‘#’ + item + ‘Sku’ ). text ;
if ( selSku != “” ) { selSku = parseSku ( selSku ) }
let selPrice = moneyToNumber ( $w ( ‘#’ + item + ‘Price’ ). text );
let selVariants = “”
console . log ( “aaaaaaaaaaaaaaaaaaaaaaaaaaaaa 221 inside for Each function: item, index :: selItem, sku, price, variants” , item , index + " :: " + selItem , selSku , selPrice , selVariants )
let count = 0
console . log ( "224 selItem = " , item , index , selItem )
**for** ( **let** group = 0 ; group < 6 ; group ++) { // checks all 6 radios groups to know which checked within each
console . log ( "227 selected index for group " + group + " = " + $w ( '#' + item + 'LabelRadio' + group ). selectedIndex )
**let** index = $w ( '#' + item + 'LabelRadio' + group ). selectedIndex // index of the selected member of this group
console . log ( "230 value = " , $w ( '#' + item + 'LabelRadio' + group ). value )
**if** ( count >> 0 && index != **undefined** ) { // avoids putting extra ";" in the record
selVariants = selVariants + " and " + $w ( '#' + item + 'LabelRadio' + group ). value ;
console . log ( "234 count, selVariants :" , count , selVariants )
} **else if** ( index != **undefined** ) {
selVariants = $w ( '#' + item + 'LabelRadio' + group ). value ;
console . log ( "237 count, selVariants :" , count , selVariants )
count = 1
}
}
**if** ( selItem != "" ) { //only insert products that were selected
acount ++
insertData ({ "sortField" : acount , "productName" : selItem , "variants" : selVariants , "sku" : selSku , "price" : selPrice })
console . log ( "244 Logging Insert:::: " , "sortField = " , acount , "productName = " , selItem , "variants = " , selVariants , "sku = " , selSku , "price = " , selPrice )
}
//console.log("inserted productName = ", selItem);
//console.log("inserted variants = ", selVariants);
//console.log("inserted Sku = ", selSku);
//console.log("inserted Price = ", selPrice);
**let** array = [ "software" , "accessory" ]
array . forEach ( **function** ( tape , index2 ) {
console . log ( "254 Im in array.forEach for software and accessories" , tape , index2 )
**let** Tape = cap ( tape )
**for** ( **let** i = 0 ; i < 6 ; i ++) {
console . log ( "257 item, index, Tape, index2 ::: " , item , index , Tape , index2 )
**if** ( $w ( '#' + item + Tape + i ). checked == **true** ){
**let** array2 = parseString ( $w ( '#' + item + Tape + "Text" + i ). text )
**let** sku = array2 [ 0 ];
**let** price = array2 [ 1 ];
**let** productName = array2 [ 2 ];
console . log ( "263 " , array2 );
acount ++
insertData ({ "sortField" : acount , "productName" : productName , "variants" : "-" , "sku" : sku , "price" : price })
console . log ( "266 Logging software accessory insert:::: " , "sortField = " , acount , "productName = " , productName , "variants = " , "-" , "sku = " , sku , "price = " , price )
console . log ( "inserted productName = " , productName );
console . log ( "inserted Price = " , selPrice );
console . log ( "inserted Sku = " , selSku );
}
}
})
})
**let** grandTotal = calcTotal ( narrowCollection )
insertData ({ "sortField" : 99999 , "productName" : "**** Grand Total ****" , "sku" : "-" , "price" : grandTotal });
console . log ( "277 calling insertData function for Grand Total :::: " , "sortField = " , 99999 , "productName = " , "**** Grand Total ****" , "sku = " , "-" , "price = " , grandTotal )
//let waitToInsert = function(){ insertData ({"sortField": 99999, "productName": "**** Grand Total ****", "sku": " " , "price": grandTotal}) }
// redirect to the Quote page where the quotation work is done.
console . log ( "Next I am redirecting to the '/quote' page" )
wixLocation . to ( "/quote" ); //This line redirects to the quote form page
}
function insertData ( toInsert ) {
wixData . insert ( “BASProducts” , toInsert )
. then ( ( results ) => {
let item = results ; //see item below
console . log ( "299 item inserted into BASProducts database is: " , item )
})
. catch ( ( err ) => {
let errorMsg = err ;
console . log ( "errorMsg = " , errorMsg )
});
}