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

I am interested to see if anyone has an example of code in wix that allows you to go through a dataset column and change all the values in the column to the same value. So, if the dataset acquired a list of numbers as it is being updated, a process can be run that changes those multiple numbers into a single one in each cell of the column in the dataset.

I understand I can run a query and get an array, then switch the numbers in the array, but I am having issues with returning the new values into the dataset.

  1. Query your collection and get items into variable called ie allItems

  2. Use for each like
    allItems.forEach((record) => {
    record.columnname = newValue
    wixData.update(“DataCollection”, record);
    });

will try this code next today. thank you.

OK, I must be doing something really dumb because the code executes but it does not update the database. Here’s the structure of the code I have setup:

on button_onclick
startquery (database)
capture result in allitems
then, for each item
acquire data from some columns in the current record:
let x = record.column value x
let y = record.column value y
use switch statement to evaluate the proper condition for next code execution
switch (value)
case z:
code calculations using value capture in earlier part of the code executes correctly (i have the result placed in a text field on the page to verify this is the case - i get bored with console.log sometimes)
within same case z - use
record.columnName = newvalue
record.columnDifferentName = newValue
wixData.update(“DataCollection”, record);

and then the switch statement falls on the default and fails to execute the new value assignments and the data collection update statement. For data collection, I use the name of the database vs. the name of the dataset on the page (which is what is used for connecting all items to the database). I’ve tried it both ways, and it produces the same result (what can I say).

I am assuming that by placing this code within a button execution on click that I am isolating it and will only execute once I click on the button (not on page load, not on page refresh, etc.)

Prior to clicking on this button, there is another button used to add a simple number to a field in the database. that works just fine. You enter the number into the field, push the submit button, and the number appears in the proper field in the database. After that is done, then you need to execute these calculations.

Any suggestions on what could be the cause for the inability to update the db with the values provided by the calculations within the switch statement?

Probably permissions on Data Collection is configured so only Admin can Update

I am the admin, that’s what puzzles me. It should work at least for me. Even in preview. I didn’t think that was a factor in the testing side of things.

In your opinion, then, the code structure itself should be OK. I don’t really understand why the switch statement, even though it correctly executes in terms of the calculations and even assigning the value to the field in code, as you suggested, fails to simply take that value and update the database.

I appreciate your comments, by the way, thank you.

Post the full Code for me to check, impossible to find Code error else

If I assume that you are correct and that permissions are at fault somehow, is there a way to tell the code to update the database regardless of permissions? To override, in other words, whatever permissions are set up? This would give me a way to test that assumption. Thank you.

@andreas-kviby

would this help? the initial step is to place a 1 or a 0 into the field hiddenFinalResults as text. The other values are initialed in a different process on the site.

@daniel20660 Hey
Ok please next time paste in code and select it and mark it as code. That makes it possible for us to copy the code and test it. Where do you get the loss of value in that code. Is it the record.profitLoss that gets empty? Change the update to

wixData.update("DailyBetList", record).then((updated) => {
  console.log(updated); // If record is updated it will console log the updated record here
})

@andreas-kviby
Next time I will share the code correctly. Apologies.

I am on the train right now. I’ll start this as soon as I am back at my desk. Thank you.

this is code:

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
 //$w("#dataset1").setFieldValue("profitLoss", myTruncatedString);
 //$w("#dataset1").setFieldValue("finalResults", "Win");
       record.profitLoss = $w("#text64").text;
       console.log("this is profitLoss at case 1",record.profitLoss);
       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
})
 break;
 case 0:
 //$w("#dataset1").setFieldValue("profitLoss", "-1");
 //$w("#dataset1").setFieldValue("finalResults", "Loss");
       record.profitLoss = "-1";
       record.finalResults = "Loss";
 //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)
  } );

}

Understood, making correction now, for the record.

for the record, placing code into comment as text. not sure how to indicate it as code in the comments system. probably should look it up.

Well if you add

wixData.update("DailyBetList", record)
  .then( (updated) => {
		console.log(updated)
	} )
	.catch( (err) => {
		console.log(err);
	} );

You should get to see if the error is a permission error

@andreas-kviby

the records ARE being updated, per your debugging code. That’s great news. I have the dataset show up on the ‘admin’ page as a table. The new values aren’t displaying on the table on the page. I am probably missing some sort of ‘update display’ command, what do you think? How do i get the most recent values on the database to display on the page properly with every update?

Here’s the screenshot of the console read after debugging code added:


The problem with the switch statement is the ‘undefined’ value of the rows where no changes have been made. That’s fine, the switch catches that and avoids a problem with calculations going nowhere.

The problem now seems to be with displaying the most recent information from the database on the page after the code has updated. I am all ears. Any suggestions?

@daniel20660 If you mark the text you will see BOLD ITALICS and so on the toolbar, to the right you have the code icon to format as code.

@daniel20660 Well, if your table is connected to a dataset on your page you just do $w(“#dataset1”).refresh(); after your update. If it is connected to a wixData query you need to take the query for another spin and then set the results to the .rows property of the table again to update it.