Loop Thru A Dataset and Update a Field

I realized that I have two different custom update forms with dropdown lists that has resulted in two different formats for a database field. I have implemented the following code to loop thru the database and update the mal-formed field:

import wixData from ‘wix-data’ ;
$w.onReady( function () {
})

export function updateAll_click(event) {
console.log( “Beginning Update…\n\n\n” )
async function loopThrough()
{
let count = $w( “#dataset6” ).getTotalCount();
console.log( "Total Count = " , count)
var results = await $w( “#dataset6” ).getItems( 0 , count);

for ( let i= 0 ; i < count ; i++) {
var payInfo = results.items[i].payment_info;
var itemID=results.items[i].itemID;
console.log( "Payment Method: " , payInfo)
// There are varying differences in the database for paypal payment method… will need to fix all update forms…
if (payInfo === “PayPal - $50” ) {
let newInfo = “PayPal : $50” ;
await updateItem(newInfo, itemID);
}
if (payInfo === “PayPal $50” ) {
let newInfo = “PayPal : $50” ;
await updateItem(newInfo, itemID);
}
if (payInfo === “PayPal - $100” ) {
let newInfo = “PayPal : $100” ;
await updateItem(newInfo, itemID);
}
if (payInfo === “PayPal $100” ) {
let newInfo = “PayPal : $100” ;
await updateItem(newInfo, itemID);
} }
}

async function updateItem(newInfo, itemID)
{
var results= await wixData.query( “Items” )
.eq( “_id” , itemID)
.find()
if (results.items.length > 0 ) {
let item = results.items[ 0 ];
item.payment_info=newInfo;
console.log(item);
await wixData.update( “Items” , item);
}
}}

========================================================
When running in Preview mode using Sandbox database it gives following errors in Java Console…

Any assistance is greatly appreciated…

Thanks
JD

Nevermind, I figured it out…

Issue #1. added loopThru function into the updateAll button click.

exportfunction updateAll_click(event) {
console.log( “Beginning Update…\n\n\n” )
loopThrough();
}

Issue#2. I forgot to change my dbname in the update function, it still read Items, so was getting error in java console that database didnt exit.

Issue#3. Finally, there was a bug in this orig code I lifted from another post…

var itemID=results.items[i].itemID;

SHOULD HAVE BEEN
var itemID=results.items[i]._id;

Once these three items were fixed. Ran it against Sandbox DB and all was well. Then ran against Live DB.

Issue resolved.

Here is the final working code for anyone that might need to change a field in an existing database…

import wixData from ‘wix-data’ ;

$w.onReady( function () {

})

export function updateAll_click(event) {
console.log( “Beginning Update…\n\n\n” )
loopThrough();
}

async function loopThrough()
{
let count = $w( “#dataset6” ).getTotalCount();
console.log( "Total Count = " , count)

var results = await $w( “#dataset6” ).getItems( 0 , count);

for ( let i= 0 ; i < count ; i++) {
var payInfo = results.items[i].payment_info;
var itemID=results.items[i]._id;

// There are varying differences in the database for paypal payment method… will need to fix all update forms…
if (payInfo === “PayPal - $50” ) {
console.log(itemID, " Record#" , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $50” ;
await updateItem(i, newInfo, itemID);
}

if (payInfo === “PayPal $50” ) {
console.log(itemID, “Record#” , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $50” ;
await updateItem(i,newInfo, itemID);
}

if (payInfo === “PayPal - $100” ) {
console.log( “itemID,Record#” , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $100” ;
await updateItem(i,newInfo, itemID);
}

if (payInfo === “PayPal $100” ) {
console.log(itemID, “Record#” , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $100” ;
await updateItem(i,newInfo, itemID);
}
}
console.log( “Update Complete.\n\n\n” )
}

async function updateItem(i,newInfo, itemID)
{
var results= await wixData.query( “CVOA_Members” )
.eq( “_id” , itemID)
.find()

if (results.items.length > 0 ) {

let item = results.items[ 0 ];
item.payment_info=newInfo;
console.log(item);
await wixData.update( “CVOA_Members” , item);
console.log( “Updated " , itemID, " Record#” , i, " With: " , newInfo);
console.log( “==========================” );
}
}

Here is the final working code…

import wixData from ‘wix-data’ ;

$w.onReady( function () {

})

export function updateAll_click(event) {
console.log( “Beginning Update…\n\n\n” )
loopThrough();
}

async function loopThrough()
{
let count = $w( “#dataset6” ).getTotalCount();
console.log( "Total Count = " , count)

var results = await $w( “#dataset6” ).getItems( 0 , count);

for ( let i= 0 ; i < count ; i++) {
var payInfo = results.items[i].payment_info;
var itemID=results.items[i]._id;

// There are varying differences in the database for paypal payment method… will need to fix all update forms…
if (payInfo === “PayPal - $50” ) {
console.log(itemID, " Record#" , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $50” ;
await updateItem(i, newInfo, itemID);
}

if (payInfo === “PayPal $50” ) {
console.log(itemID, “Record#” , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $50” ;
await updateItem(i,newInfo, itemID);
}

if (payInfo === “PayPal - $100” ) {
console.log( “itemID,Record#” , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $100” ;
await updateItem(i,newInfo, itemID);
}

if (payInfo === “PayPal $100” ) {
console.log(itemID, “Record#” , i, " Payment Method: " , payInfo);
let newInfo = “PayPal : $100” ;
await updateItem(i,newInfo, itemID);
}
}
console.log( “Update Complete.\n\n\n” )
}

async function updateItem(i,newInfo, itemID)
{
var results= await wixData.query( “CVOA_Members” )
.eq( “_id” , itemID)
.find()

if (results.items.length > 0 ) {

let item = results.items[ 0 ];
item.payment_info=newInfo;
console.log(item);
await wixData.update( “CVOA_Members” , item);
console.log( “Updated " , itemID, " Record#” , i, " With: " , newInfo);
console.log( “==========================” );
}
}

Just dropping a big thank youuuuuuu!! I got exactly what I needed