Using code to change value of all fields in a dataset column

@andreas-kviby

The table on the page is connected to the dataset1 that is generated by the database. I will set up a refresh() after the update command and see where that goes. My assumption is that the table displaying the data will refresh to whatever the latest version of the data is.

@daniel20660 Yes but to force it use $w(“#dataset1”).refresh() and it will read in data again and refresh table. Hope it finally helps you to make your thing work. If you like my help and the solution works please set Top Comment as it will help me in the forums.

I have changed the code to include the wixdata force update statements. I am testing the use of the page as ‘stupid user’, moving back and forth on the dataset, changing the initial entry and then going on to the second one, and I am getting this kind of error:


Here’s what the code looks like after the changes including the force update:

export function button7_click(event) {
///calculates the profit/loss after someone has entered 1 or 0 into the hidden final results field in step 1
wixData.query("DailyBetList")
  .find()
  .then( (results) => {
let totalCount = results.totalCount;
let allItems = results.items;
//console.log("this is total count",totalCount);
allItems.forEach((record) => {
//record.columnname = newValue
//wixData.update(“DataCollection”, record);
 //let currentItem = results.items[0]; //see item below
 let value = record.hiddenFinalResults;
 let valueRecommendedBetOdds = record.recommendedBetOdds;
 var value1 = Number(value);
 switch(value1) {
 case 1:

//calculate the profit loss
 var numRBO = parseFloat(valueRecommendedBetOdds);
 var num = (numRBO*value1)-value1; //if value is "1" then it is a win so calculate the profitLoss
 var n1 = num.toString(); //turn the result of that calculation into a string
 var length = 4;
 var myString = n1;
 var myTruncatedString = myString.substring(0,length);
        $w("#text64").text = myTruncatedString;

 ////added for debugging purposes
 //console.log("this is my truncated string",myTruncatedString);
 
 ///place items in the database
       record.profitLoss = $w("#text64").text;
       record.finalResults = "Win";
 //wixData.update ("DailyBetList", record);
       wixData.update("DailyBetList", record).then((updated) => {
        console.log(updated); // If record is updated it will console log the updated record here
      })
      $w("#dataset1").refresh();
 break;
 case 0:
 //$w("#dataset1").setFieldValue("profitLoss", "-1");
 //$w("#dataset1").setFieldValue("finalResults", "Loss");
       record.profitLoss = "-1";
       record.finalResults = "Loss";
       wixData.update("DailyBetList", record).then((updated) => {
        console.log(updated); // If record is updated it will console log the updated record here
      })
      $w("#dataset1").refresh();
 //wixData.update ("DailyBetList", record);
 break;
 default:
       console.log("there is a problem with the switch statement")
 //$w("#dataset1").setFieldValue("profitLoss", "0");
 break;
}

});


 } )
  .catch( (err) => {
 let errorMsg = err;
    console.log("this error shows up", errorMsg)
  } );

}

Here’s the part of the interface that starts the operation:


I haven’t added any code to step one, and am simply using the button as a submit button. it submits a 0 or a 1 to hiddenFinalResults, to enable step 2 to happen.

any possible idea why the save commands seem to be acting up? how do I better manage it to enable ‘stupid user’ to enter data without being worried about moving back and forth through the dataset.

Thank you in advance for any advice.

Maybe yiou need to move the $w(" #dataset1 ").refresh(); into the returned promise after update so you do not refresh dataset during updating operations?

Done and it worked. everything is going the way it is supposed to for now.