wixData.update Need Help

Hello!
In my database (forexample “MyDatabase”) there are several fields. For example
title,
name,
order number

I make filterSelection by “title”. Input values for filtering.
For example: One result - 10 records, another - 98
I put this numbers in variable “CountResult”

I need update database just put in field “orderNumbers” numbers from 1 to “CountResult”

I do Query, read Results with selected filter
and run FOR (var i=0, I<CountResults, I++)

Inside do qwery.update

When I updating (10 rows records) It’s working and updating
With 98 results - about 30 Errors, About 30 Rows not Updated
Each attemprs - give me different wrong rows and No Error Message, Just word :Error"

Please advice me in such code.
I repeat - Just make query, read result and change on columns - out in it numbers from 1 to “CountResult”

Thank You in advance
I do

My code fragment:
//SelectKey - selected filtering Item
// SongsCount - RealNumber, determined in code before

wixData.query(“dbBardSongs”)
.eq(“bardName”, selectedKey)
.limit(100)
.find()
.then( (results1) => {
if (results1.items.length > 0) {

for ( var i=0;i<SongsCount;i++) {
toUpdate = {
“_id”: results1.items[i]._id,
“title”: results1.items[i].title,
“bardName”: results1.items[i].bardName,
“firstLine”: results1.items[i].firstLine,
“orderNumber”: i + 1
};
wixData.update(“dbBardSongs”, toUpdate)
.then( (results2) => {
item = results2;
};
. catch ( (err) => {
errorMsg = err;
console.log("error Msg = " + errorMsg);
} );

}
else {
// handle case where no matching items found
}

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

What is SongsCount and why not to use bulkUpdate in this case?

SongCount - Real number of Records (number of Rows for each BardName) which changed daprnd of filter - I count it before - this is just real Number like 10 pr 30 or 55…

have about 200 BardsName and about 10 or 100 sings name which each of them… I need to make this OrderNumber for somrthing else, but i stock with Updating Error

I don’t know about bulkUpdate… I’m beginner and studying step by step. I’m afraid I make mistake in CallWaiting may be in {} may be with .then {}

Will be appreciate with this tack… Updating one database column with increasing numbers

is SongsCount equal to results1.items.length ?

In a code befiore after Set filter by Name of person

 $w("#datasetBardSongs").setFilter(wixData.filter().eq("bardName", selectedKey)) 
      .then( () => {SongsCount = $w("#datasetBardSongs").getTotalCount();     

Ерш is just Real Number.
My problem not in it… This is just 5 or 15 or 55

OK. But if somehow SongsCount > results1.items.length then you’ll get errors when you try to update. So make sure that’s not the case.

As I tel You - When SongCount 10 - All10 Raws updated in columns “Ordernumber” I see numbers from 1 to 10, but when SongCount 98 Some Raws updated and some not - and iit happend randomly. Forexamplq I see numbers from 1 to 45 then skeeped 5 number than 10 numbers from 50 to 60 and ETC… Problem in Database updated… Some raws just not updated

As I said, you should make sure the results1.items.length <= SongCount
If you verified that’s the case, then I don’t know what the problem is. But if the results are less then songCount then errors are expected. Why won’t you add acondition to your loop just to be safe:
for ( var i=0; i<Math.min(SongsCount, results1.items.length); i++) {
}

they are the same… SongCount - number of Auery Raws with SetFilter

also it seems you forgot to close the parentheses of the “then” block of the update promise.

I mean:

wixData.update(“dbBardSongs”, toUpdate)
.then( (results2) => {
item = results2;
} ) ;

It’s look like some raws just “Dropped”… Not updated… May be Server refuse because of speed to writing or something else…

and remove the semicolon if you have .catch() after it.

I double chacking all “open” “close” elements - and do not see mistakes, I do it again… But As i weite before - with 10 rows it working, mistake shown with 98 rows. Ok - I try to add as You adviced line and triple check all open-close moment and come back.

Thank You

Maybe. It’s difficult to say. I would try to use the looping to create an array of objects and then submit them at once using bulkUpdate (if you go for it, in every loop cycle push Object.assign to the array and not the object itself).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Thank You - I wil check what You write tonight… And will return with any update

I look carefully on my code, Use real numbers instead identification and Deferentially see that Database Update “lost” (skip, drop) some rows during update if I update more then 20 rows one by one

May be timeout of owerstok or or???