For Loop Error: Couldnt save during save

Hello, im trying to make code that goes through a database with about 90 items in it and automatically applies a letter based off the rarity of the item is. I want it to label it for me however when i run the code it only runs for 1 item and then when it attempts to go to the next item it gives me an error.

Here is my code:

$w("#StartButton").onClick(()=>{
    let count = $w("#dataset1").getTotalCount(); // 23
    var v = 0;
for (var i=0;i<count;i++) {
    
$w("#dataset1").onReady( () => {
    $w("#dataset1").setCurrentItemIndex(i);
});

    let itemObj = $w("#dataset1").getCurrentItem();
var itemRare = itemObj.rarity;
var name = itemObj.title;

console.log("Item num " + i + " Rarity: " + itemRare + " Name: " + name)

if (itemRare == "Common") {
$w("#dataset1").setFieldValue("sortTitle", "Z");
$w("#dataset1").save();
console.log("ITEM SAVED ");

}
if (itemRare == "Uncommon") {
$w("#dataset1").setFieldValue("sortTitle", "L");
$w("#dataset1").save();
console.log("ITEM SAVED ");

}
if (itemRare == "Rare") {
$w("#dataset1").setFieldValue("sortTitle", "K");
$w("#dataset1").save();
console.log("ITEM SAVED ");

}
if (itemRare == "Epic") {
$w("#dataset1").setFieldValue("sortTitle", "J");
$w("#dataset1").save();
console.log("ITEM SAVED ");

}
if (itemRare == "Legendary") {
$w("#dataset1").setFieldValue("sortTitle", "J");
$w("#dataset1").save();
console.log("ITEM SAVED ");

}
if (itemRare == "Shadow Series") {
$w("#dataset1").setFieldValue("sortTitle", "I");
$w("#dataset1").save();
console.log("ITEM SAVED ");

}
if (itemRare == "Icon Series") {
$w("#dataset1").setFieldValue("sortTitle", "C");
$w("#dataset1").save();
}
if (itemRare == "Star Wars Series") {
$w("#dataset1").setFieldValue("sortTitle", "G");
$w("#dataset1").save();
}
if (itemRare == "DC SERIES") {
$w("#dataset1").setFieldValue("sortTitle", "F");
$w("#dataset1").save();
}
if (itemRare == "MARVEL SERIES") {
$w("#dataset1").setFieldValue("sortTitle", "E");
$w("#dataset1").save();
}
if (itemRare == "DARK SERIES") {
$w("#dataset1").setFieldValue("sortTitle", "D");
$w("#dataset1").save();
}
if (itemRare == "Lava Series") {
$w("#dataset1").setFieldValue("sortTitle", "B");
$w("#dataset1").save();
}
if (itemRare == "Frozen Series") {
$w("#dataset1").setFieldValue("sortTitle", "A");
$w("#dataset1").save();
}
}

i++

});

I’ve tried to search it up online however there’s no simple fix that works for my situation. Is it possible to fix it?

Why are you not saving after the for loop?

You have several errors:

  1. onClick has to be inside $w.onReady block

  2. dataset.onReady should not be inside the loop

  3. dataset.onReady should not beinside an event handler (it may get ready prior to the click

  4. Instead of saving on loop, you’re better change all the items you need and then save them all together with a direct bulkUpdate (then dataset.refresh() to reload the contents).
    https://www.wix.com/velo/reference/wix-data/bulkupdate
    https://www.wix.com/velo/reference/wix-dataset/dataset/refresh

Im not sure how to do that

Ive removed the dataset save and put it after.


It just gave me new errors

@david12252006 As J.D said, there are multiple errors. And, the save is still in the for loop, you just moved it out of the if statements.