Truncation problems

I have a database called “BAS Products” that I create each time a visitor makes selections from my own custom page (not using Wix Stores for this). The database is first Truncated to clear out all data, then the user makes his product selections. When he presses a button to request the quote, the button code populates the “BAS Products” database with his product selections, and automatically redirects to the Quote page. On the Quote page, the database is automatically displayed in a Wix Table. The database is also read and copied into a “Request” database. The Request database is my permanent record of all product quotation requests. Everything works perfectly in Preview mode, but fail in in various ways in Publish mode. Note, during development I have the sandbox turned off so I am not dealing with forgetting to sync up.

I have tried everything I know (am not an expert) and all have failed wo resolve. I tried using Async for the truncate function. I tried changing permissions from Site Content to Private. I have tried putting the Truncate in the Backend with “suppressAuth” turned on. That will truncate, but will not allow the code to pupulate the database. It is like there is a block on adding data. Nothing seems to work in publish mode and ALL attempts work in Preview mode. Frustrated now after 5 days of working on this.

I realize I may have confused some but I tried to explain this best I can. Happy to answer any question you might have for clarification. I am looking for some ideas of what I might try next.

I am beginning to think this has to be a bug because it works in Preview (everything I tried works there) but NOT in Publish mode.

Thanks for your help.

I hear your frustration. I think the issue is security. When you work in preview mode, you work in admin mode. When you working live, quite often you are just a user with no permissions. Are you using the backend to execute some of your code? or are you doing everything in the frontend. (your webpage code).
Can you post the code so we can see it. There are tricks to use the backend to read/write to to your table.

Second if you want to just have the user add data to the table, you can set the security mode on the table to Formpublish. This will allow any user to write to the table, but they can’t change or view it. Since your backend code is working for the truncating, I’m assuming you have the security overriding working properly. I just wrote up a frontend/backend for someone else. The comments are lengthy, but the code is there if you want to look. Usually something small causes the hangups.

You can look here for inspiration. The code I posted, does work, debugged and tested it. : https://www.wix.com/velo/forum/coding-with-velo/login-redirect-1

If you want to post the code, I can review it for you.

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 )
});
}

What if you try:
wixData . truncate ( “BASProducts” ,[{ suppressAuth : true }])

The api shows the options as an array. I put square brackets (array[]) around the statement.

wixData . truncate ( “BASProducts” ,[{ suppressAuth : true }])

This results in a pre-compile error:
Type ‘{ suppressAuth: boolean; }[]’ has no properties in common with type ‘WixDataOptions’

I tried other combinations of placing the square brackets to no avail.

@break80, I went back and looked how I used it in my code and I had quotes around suppressAuth. I see that I DID NOT use the square brackets.

let options = {
“suppressAuth”: true,
“suppressHooks”: true
};

That did not help. Save perfect result form Preview mode but fails in Publish mode. Did you say in earlier posting you thought my problem is related to permissions and you knew of some tricks?

Maybe it might be easier to talk. I can try things that you think of. I am out if ideas.

I am still getting this error in site monitoring…

When I click the error in the site monitor, the JSON of the response is very long…this is the jsonPayload part. I searched everywhere for some indication how to make sense of this. I presume it is a message from the compiled internal code, not the code I am using. Do you know how to read this?

“jsonPayload”: {

“message”:“TypeError: Illegal invocation at I (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:15237) at h (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:16873) at Object.get text [as text] (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:17236) at Object.keys.reduce.c.value.c.get.d.get [as text] (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:56474) at https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/97824228-dc79-4f79-9624-67ed4729aa73/pages/gkutr.js?dependencies-token=3938:1:7348 at Array.forEach () at https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/97824228-dc79-4f79-9624-67ed4729aa73/pages/gkutr.js?dependencies-token=3938:1:7110 at Array.forEach () at _ (https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/97824228-dc79-4f79-9624-67ed4729aa73/pages/gkutr.js?dependencies-token=3938:1:6070) at e. (https://static.parastorage.com/services/wix-code-viewer-app/1.1479.164/app.js:25:3915)”
}

I did an in-depth comparison of the site monitor output and what is happening in the database.

  1. The database IS truncating ok.

  2. There are two parts to the code I posted earlier. The first part is working and filling BASProducts.

  3. The 2nd part (from here forward::: let array =[ “software” , “accessory” ] array . forEach ( function ( tape , index2 ){ ) is not executing. Execution stops just before this code. There must be something about this code that is failing.

I am looking at your code in Visual Code. Not done yet but found this missing so far.

$w . onReady ( function () {
//console.log("…in $w.onReady…: ")
// other unrelated code here
runClear ()
console . log ( “clear BASProducts in back end” )
}) ----<

There was a missing }) for your onReady function. Still look at the rest of your code.

I got this from reviewing the code you posted earlier. I’m sure you caught that, because I dont’ see how anything would have run if that was still missing.

Rest of the code looks good. I can’t see any other issues. All the if blocks, foreach blocks etc are terminated properly

@break80, I agree in your assessment as to where the error is occurring. I absolutely love your generous use of console.log messages. That is exactly how I debug also.

Time for more console.log messages.

I actually think the error is between
console . log ( "257 item, index, Tape, index2 ::: " , item , index , Tape , index2 )

and the end of the function. Because line 257 showed up in your log. You have 3 places where insertData can be called, so we need more console.log()s. Yeah!!!.

Put 1 at 259, to ensure you entered the if block., Put one before each insertData.

We will slay this dragon yet!!!

a

The dragon is going down. I figured it out. The problem is with the parseString code (you have not see that), or at least related to it. That error message I was asking about …


was the key. If I could only have understood where it came from. Since the code runs synchronously, the console.logs are not in the order one would expect. Do you know of a way to figure that out?

Anyway, I modified the code, converting the suspect part of the code into a separate function. They added lots of console logs. That helped me home in on it.

I commented the “let array2” statement and added a fake one that looked like what I would expect back from the parseString function. Problem solved almost.
//let array2 = parseString($w(‘#’ + item + Tape + “Text” + i).text)
let array2 = [ “22222222” , “5000” , “fakeName” ]

Wow! What a relief! I am calling it a day for now, but at least I know where to look for the issue. Posting the function now. Do you see anything wrong in it?

Thank you so much for helping me get off dead center.

function parseString ( str ) {

let  ar = str . split ( ";" ); 
console . log ( "161 " , ar ) 

let  skuRaw  =  ar [ 0 ] 
let  priceRaw  =  ar [ 1 ] 
let  productName  =  ar [ 2 ] 

console . log ( "167 skuRaw = " , skuRaw ) 
console . log ( "168 priceRaw = " , priceRaw ) 

let  skuRaw2  =  skuRaw . split ( ":" ) 
let  priceRaw2  =  priceRaw . split ( ":" ) 

console . log ( "173 skuRaw2 = " , skuRaw2 ) 
console . log ( "174 priceRaw2 = " , priceRaw2 ) 

let  sku  =  skuRaw2 [ 1 ]  
let  price  =  priceRaw2 [ 1 ]  

sku  =  sku . trim (); 
price  =  price . trim (); 
productName  =  productName . trim () 

price  =  moneyToNumber ( price ) 

console . log ( "185 sku = " , sku ) 
console . log ( "186 price = " , price ) 
console . log ( "187 productName = " , productName ) 

let  array  = []; 
array [ 0 ] =  sku ; 
array [ 1 ] =  price ; 
array [ 2 ] =  productName ; 

**return**  array 

}

No problems, just glad you got it!!! Great work. Eyeballing the function parseString, I don’t see anything glaring. It looks like it should work. My coding style is I tend to combine several steps at once, but then I can’t do console.log() as granularly as you can. But you’ve covered all the steps, each to his own coding style. My motto is, if it works then your job is done.

I was looking at the output of the console log, and the oldest is at the bottom and the newest is at the top. I saw Line 257 at the bottom, so I knew you got past that line at least.

The dump you got from Java was a stack dump and it usually goes in reverse order. When it displays the dump, the location where the issue started is usually at the bottom. The dump order is innermost to the outermost call.

For example if your ForEach calls, step2, step2 calls step3, which calls step4 and so on, then the dump comes out in order of 4,3,2,1. So the problem is somewhere near the bottom of the dump.

You also need to parse the java stack trace dump out a bit to decipher it. You start by looking for the ‘at’ word and break the lines there. I did that for your stack trace. If you want you can copy this into a better editor where you can better see the list.

“message”:"TypeError: Illegal invocation
at I (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:15237)
at h (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:16873)
at Object.get text [as text] (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:17236)
at Object.keys.reduce.c.value.c.get.d.get [as text] (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.db0da9b0.bundle.min.js:2:94402), :3:56474)
at https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/97824228-dc79-4f79-9624-67ed4729aa73/pages/gkutr.js?dependencies-token=3938:1:7348
at Array.forEach () at https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/97824228-dc79-4f79-9624-67ed4729aa73/pages/gkutr.js?dependencies-token=3938:1:7110
at Array.forEach () at _ (https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/97824228-dc79-4f79-9624-67ed4729aa73/pages/gkutr.js?dependencies-token=3938:1:6070)
at e. (https://static.parastorage.com/services/wix-code-viewer-app/1.1479.164/app.js:25:3915)"

Your error occurred in the Array.forEach section.

Anyways good luck with your project.

I am closer now but still puzzled. Maybe you can see something I am missing. I homed in closer, to the record that is causing the issue. It is not the parseString, but the page element that IS the string is wrong. The statement that grabs the page element is failing in publish mode but not in preview mode.

I tested using the raw page element in publish mode to try to see what is happening…

This is the page element whose text is $w(‘#instrumentAccessoryText0’).text

This is the code where Publish stops with the error message

The Site Monitoring tool gave me this…

This is the error payload…
“jsonPayload”: {

“message”:“TypeError: Illegal invocation at I (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.a98e3e65.bundle.min.js:2:98056), :3:15237) at h (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.a98e3e65.bundle.min.js:2:98056), :3:16873) at Object.get text [as text] (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.a98e3e65.bundle.min.js:2:98056), :3:17236) at Object.keys.reduce.c.value.c.get.d.get [as text] (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.a98e3e65.bundle.min.js:2:98056), :3:56474) at https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/4d6b61d4-928c-450e-9945-7f9ef17a861d/pages/gkutr.js?dependencies-token=3938:1:6984 at Array.forEach () at _ (https://bundler.wix-code.com/f5624679-9c3d-4fff-bd20-001da4d1091d/ad41a5b7-ef45-4da3-bc5e-80473e63f59f/4d6b61d4-928c-450e-9945-7f9ef17a861d/pages/gkutr.js?dependencies-token=3938:1:5890) at e. (https://static.parastorage.com/services/wix-code-viewer-app/1.1479.164/app.js:25:3915) at eval (eval at evaluateBundle (https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.a98e3e65.bundle.min.js:2:98056), :3:1020) at https://jimyaccino.wixsite.com/_partials/wix-thunderbolt/dist/clientWorker.a98e3e65.bundle.min.js:2:344576
}

Clearly the p00String is failing…line 255

Can you see anything wrong?

Paul, this is unbelievable! I found the solution, and now believe it is a WIX bug. The semi-colon ( ; ) is the problem.

I have used the semi-colon as a delimiter in a concatenated string, such as here in a text field set next to a checkbox.

When I changed the delimiter from semi-colon to three periods ( … ), the problem was cured. The dragon had been slayed (or whatever)!

Can someone from Wix confirm or deny this is a bug? If not, please show me the reference that says NOT to use semi-colon as a delimiter in a string.

All I can say to you sir is, Man, you like a dog with bone. Once you’re onto something, you don’t let go until you have got it figured out. Congrats!!!
Anyway I’m off to bed, it 1:15AM here.