I have a list of n items that need to be inserted into my database. When an item is inserted I have to give it the next boxNumber available by adding 1 to the previously saved result. The problem is that Wix does not seem to be completing the write before resolving the promise or await as I often get the same boxnumber result for the next insert. My code is below
To insert item, I loop through them and run
result = await wixData.insert('Items', item);
return result;
The problem is probably in the looping or the async function that you haven’t posted here.
Anyway you can use a bulkInsert instead of many single inserts.
to do that you can add to your itemsToSave array a key tempIndx which its value is equal to it’s array index +1;
then run a bulk insert and on the backend:
You’re welcome.
If you’re going to use insert in other cases without a bulkInsert (and then you won’t have tempIndx), then make sure to use the condition as I put above (+write code to support cases for empty databases in both cases: bulk and non bulk inserts).