Dataset not operating as expected

I’m trying to pull data from my dataset, add a value to it, then save it.
I’m expecting it to do just that, but it’s like it’s pulling outdated information on the next save.

For reference, the “New DB Values” lines. (format = value from dataset : value to add : dataset value + value to add):

Console

You can see by comparing the two, reachoutsDaily updated, but reachoutsMonthly / Weekly / Total didn’t. Sometimes the updated values are sometimes lower than the last data written which mind boggles me. Even if I wait like 10 seconds between writes, it still happens.

Here’s the code. I know I probably should have some kind of bulkUpdate impl but this was a quick solution.

Also, I’m new to coding and Velo.

Also here’s where the functions are being called.

For anyone else who has this issue, it was a race condition.

It was solved by chaining method calls and making sure no update operations run in the middle of another update operation.

Hi Aaron,

Maybe add the new working code so people can see what went wrong and how to fix it.

Kind regards,
Kristof.

Now, although I don’t quite understand the question.The issue
Sounds like it’s from using wixData.save() . Try using wixData.insert().

"The save() function returns a Promise that resolves to the inserted or updated item after it has been added or updated in the specified collection."

For more detailed information
https://www.wix.com/velo/reference/wix-data/save

the issue was because i would call the updateStatitsitc() method 4 times at the same time. since the save/update tasks are async, it was pulling data from the query and saving out of order

https://en.wikipedia . org/wiki/Race_condition#Example

@volkaertskristof conveniently editorx wiped the last 4 days of my work so i’ll have to come back and put it here when i get around to doing that work again.

The issue isn’t really “race condition”, rather it’s the need to handle Promises returned from asynchronous services. See the following for more information about Promises: