wixData SAVE not "saving/updating" reliable in live site.

I have a repeater with toggle buttons which allow the user to show or hide their item(s) on their menu. The problem is that on “preview” the code works flawlessly, but in “live”, unreliable. Sometimes it works, and sometimes does not. I’ve already checked the live DB permissions (anyone), tried timeouts, and nothing. Also, tried to do a query after save but nothing either. Could someone help please?

import wixData from ‘wix-data’ ;

let switchButtonVal = false ;

$w.onReady( function () {

});

$w.onReady( async () => { //Menu repeater
const buttonRepeater = $w( “#menuRepeater” );
buttonRepeater.onItemReady(($w, itemData, index) => {

    $w( "#showHideBtn" ).onClick((event) => {  //Add button on each menu item 

if ($w( “#showHideBtn” ).checked) {
switchButtonVal = true ;
} else {
switchButtonVal = false ;
}

let itemID = $w( “#partnerDataset” ).getCurrentItem()._id;
let cat = $w( “#partnerDataset” ).getCurrentItem().category;
let itemName = $w( “#partnerDataset” ).getCurrentItem().dishName;
let desc = $w( “#partnerDataset” ).getCurrentItem().description;
let price = $w( “#partnerDataset” ).getCurrentItem().price;
let pic = $w( “#partnerDataset” ).getCurrentItem().picture;
let catNum = $w( “#partnerDataset” ).getCurrentItem().categoryNumber;

let toUpdate = {
“_id” : itemID,
“category” : cat,
“dishName” : itemName,
“description” : desc,
“price” : price,
“picture” : pic,
“categoryNumber” : catNum,
“showHide” : switchButtonVal
};

//console.log("line 43: index: " + index);
wixData.save( “TerraventoDB” , toUpdate)
.then( (results) => {
let valueChecked = showHideDoubleCheck(itemID, index)
} )
. catch ( (err) => {
let errorMsg = err;
} );
});
});
});

function showHideDoubleCheck(itemID, index, valueChecked) {
wixData.query( “TerraventoDB” )
.eq( “_id” , itemID)
.find()
.then( (doubleCheckResult) => {
valueChecked = doubleCheckResult.items[index].showHide;
return valueChecked;

    } ) 
. **catch** ( (err) => { 

let errorMsg = err;
} );
return valueChecked;
}

Anybody here that could please help on this…?

When using catch error, you want to know the error:

.catch( (err) => {  let errorMsg = err;

can you use console.log(err) to get the error? It helps people to see the bug. You can see the console log in the browser console.

You do a .save() and immediately afterwards a query to doublecheck the results. Due to infrastructural lag, the .save() has not been propagated yet to all “copies” inside Wix’s CDN. Solution: try setting a timeout for 250 ms before you do the query, that should work.

Just tried the console.log but it is not throwing any error; it just didn’t sorted like if “.ascending(parameter)” code was never there without errors.

Thanks for replying. The query should be inside the save’s “.then”?

let toUpdate = {
     "_id": itemID,
     "category": cat,   
     "dishName": itemName,
     "description": desc,
     "price": price,
     "picture": pic,
     "categoryNumber": catNum,
     "showHide": switchButtonVal
};
wixData.save("MyDB", toUpdate)
    .then( (results) => {
         //THE QUERY SHOULD BE HERE OR WHERE???
     } )
.catch( (err) => {
      let errorMsg = err;
      console.log("ERROR: " + err);
 } );

@hjgr
By the advice of Giri Zano…

.then((results)=>{//SHOULD BE THE RIGHT PLACE})
.then((results)=>{
    setTimeout(()=>{
        start_myNextFunction();
    },250)
})

function start_myNextFunction() {
    //do you next step here...
}

@russian-dima Right now I’m trying your suggestion and still struggling, but on the other hand, still wondering why in sandbox it’ll work at all times, but not in live. Obviously I don’t know your architecture, but it is not making sense to me right now. Please, advice. I’ll keep trying.

Try this:

function showHideDoubleCheck(itemID, index, valueChecked) {
	fnHoldOn(250);
    wixData.query("TerraventoDB")
        .eq("_id", itemID)
        .find()
        .then( (doubleCheckResult) => {
            valueChecked = doubleCheckResult.items[index].showHide;
 return valueChecked;
/etc ....

function fnHoldOn(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

And whatever you do DO NOT rename the function fnHoldOn to “sleep”. I called it that at the beginning, and it took me out of action for 3 weeks. Turned out that “sleep” was wrongly tagged by wix as sql-injection (which has a sleep-statement) and connection was immediately closed.

EDIT: about your question on Sandbox vs. Live: it could be that Wix synchronizes the Sandbox first and then Live, which would explain your dilemma. But I have little experience with Sandbox, don’t use it and am very glad you can turn it off now and work in Live all the time.

While trying that, there is another issue that I can’t even know how to replicate. Around 10% of times, the wixData.update code updates wrong columns with values that goes to another columns, see here:

In the picture, I ran the code the first time and it worked (first row). It did what I need it to do, which is to update Show/Hide value as true or false. But the 2nd time I ran it, the value that goes to Show/Hide column went to Category Number column, and so forth the Category Number value into Picture… I’m digging on this, but again, this is on Live site… it works flawlessly on sandbox.

Hi Giri Zano,

I just disabled the sandbox and ran the exact same test as the picture above and got the exact same results. At this point, I’m reaching a dead end; not really sure what else to do.

I would appreciate of somebody give me some light on this.